import numpy as np
import pandas as pd
from enum import Enum
from copy import deepcopy

MAX_OUTS = 3
MAX_RUNS_PER_INNING = 5
MAX_INNINGS = 4

class Player:
    p1b = 1.0
    p2b = 1.0
    p3b = 0.0
    phr = 0.0
    pso = 0.0 #struck out
    pbo = 0.0 #hit but thrown out at first (useful for advancing a player)

    def __init__(self, hit_probs):
        self.p1b, self.p2b, self.p3b, self.phr, self.pso, self.pbo = hit_probs
        if ((self.p1b + self.p2b + self.p3b + self.phr + self.pso + self.pbo) != 1.0):
            print("Error with setting player")
            print(hit_probs)
            print((self.p1b + self.p2b + self.p3b + self.phr + self.pso + self.pbo))
        self.hit_probs = [self.p1b, self.p2b, self.p3b, self.phr, self.pso, self.pbo]
    def get_hit(self):
        return np.random.choice(['p1b','p2b','p3b','phr','pso','pbo'], 1,
                           p = self.hit_probs)

class BaseBallAction(Enum):
    Single = 1
    Double = 2
    Triple = 3
    HomeRun = 4
    StrikeOut = 5
    ThrownOut = 6

class BaseballState():
    def __init__(self, inning, runs, outs, base3, base2, base1, inning_start_runs = 0.0, likelihood = 1.0):
        self.inning = inning
        self.runs = runs
        self.inning_start_runs = inning_start_runs
        self.outs = outs
        self.base3 = base3
        self.base2 = base2
        self.base1 = base1
        self.likelihood = likelihood
    def __repr__(self):
         return "Inning: {}, Runs: {}, Inning Start Runs: {}, \n                  Outs: {}, First: {}, Second: {}, Third: {}, \n                  Likelihood: {}".format(self.inning, self.runs, self.inning_start_runs,                                         self.outs, self.base1, self.base2, self.base3, self.likelihood)

    def reset_bases(self):
        self.base1 = 0
        self.base2 = 0
        self.base3 = 0

    def increment_inning(self):
        self.inning = self.inning + 1
        self.reset_bases()
        self.outs = 0
        self.inning_start_runs = self.runs

    def advance_bases(self, count, new_runner):
        while(count != 0):
            if (self.base3):
                self.runs = self.runs + 1
                self.base3 = self.base2
                self.base2 = self.base1
                self.base1 = new_runner
                #max runs advance inning
                if ((self.runs - self.inning_start_runs)%MAX_RUNS_PER_INNING == 0):
                    self.increment_inning()
                    break
            else:
                self.base3 = self.base2
                self.base2 = self.base1
                self.base1 = new_runner
            new_runner = 0 #only new runner first time through
            count = count - 1

    def next_state(self,action, action_probability):
        new_state = deepcopy(self)
        new_state.likelihood = new_state.likelihood*action_probability
        if (action == BaseBallAction.StrikeOut) or (action == BaseBallAction.ThrownOut):
            new_state.outs = new_state.outs + 1
            if (new_state.outs == MAX_OUTS):
                #max outs advance inning
                new_state.increment_inning()
            elif (action == BaseBallAction.ThrownOut): #don't always advance base but will here...also usually throw to first
                new_state.advance_bases(count = 1, new_runner = 0)
        elif (action == BaseBallAction.Single):
            new_state.advance_bases(count = 1, new_runner = 1)
        elif (action == BaseBallAction.Double):
            new_state.advance_bases(count = 2, new_runner = 1)
        elif (action == BaseBallAction.Triple):
            new_state.advance_bases(count = 3, new_runner = 1)
        elif (action == BaseBallAction.HomeRun):
            new_state.advance_bases(count = 4, new_runner = 1)
        return new_state

'''
    Runs a multi-hypothesis set of scenarios to determine likelihoods for each final state ate end of game
    This looks like a tree once it is completed where the leaf nodes are the possible final states
'''

def run_player_scenario_mh(all_players, order):
    players = [all_players[i] for i in order]

    runs = [0.0 for i in range(0,MAX_INNINGS*MAX_RUNS_PER_INNING + 1)]
    def run_next_iter(cur_state, player_idx, baseball_action, probability):
        new_state = cur_state.next_state(baseball_action, probability)
        if(new_state.inning > MAX_INNINGS):
            runs[new_state.runs] = runs[new_state.runs] + new_state.likelihood
        else:
            run_next_player(new_state,(player_idx + 1)%len(players))

    def run_next_player(cur_state, player_idx):
        current_player = players[player_idx%len(players)]
        if(current_player.p1b > 0.0):
            run_next_iter(cur_state,player_idx,BaseBallAction.Single, current_player.p1b)
        if(current_player.p2b > 0.0):
            run_next_iter(cur_state,player_idx,BaseBallAction.Double, current_player.p2b)
        if(current_player.p3b > 0.0):
            run_next_iter(cur_state,player_idx,BaseBallAction.Triple, current_player.p3b)
        if(current_player.phr > 0.0):
            run_next_iter(cur_state,player_idx,BaseBallAction.HomeRun, current_player.phr)
        if(current_player.pso > 0.0):
            run_next_iter(cur_state,player_idx,BaseBallAction.StrikeOut, current_player.pso)
        if(current_player.pbo > 0.0):
            run_next_iter(cur_state,player_idx,BaseBallAction.ThrownOut, current_player.pbo)

    start_state = BaseballState(1,0,0,0,0,0)
    state_player_idx = 0
    run_next_player(start_state, state_player_idx)

    w_sum = 0
    for idx, val in enumerate(runs):
        w_sum = w_sum + val*idx
    #return w_sum
    return MAX_INNINGS*MAX_RUNS_PER_INNING - w_sum

'''
    Runs a a monte-carlo using the players to figure out statistical return
'''

def run_player_scenario_mc_standalone(all_players, order):
    players = [all_players[i] for i in order]

    df = pd.DataFrame(data = [], columns = ['Inning', 'Runs', 'Outs', 'Inning Runs'])
    OUTS = 3
    MAX_RUNS_PER_INNING = 5
    MAX_INNINGS = 4
    MAX_MC_RUNS = 100

    for sim_runs in range(0,MAX_MC_RUNS):
        runs = 0
        inning = 1
        current_batter = 0
        bases = [0,0,0] #b1, b2, b3
        outs = 0
        start_runs = 0

        while inning <= MAX_INNINGS:
            batting = True
            result = players[current_batter % len(players)].get_hit()
            current_batter = current_batter + 1
            if (result == 'pbo') or (result == 'pso'):
                outs = outs + 1
                if (outs == 3):
                    #max outs advance inning
                    df.loc[sim_runs*MAX_INNINGS + inning] = (inning, runs, outs, runs - start_runs)
                    inning = inning + 1
                    bases = [0,0,0]
                    outs = 0
                    start_runs = runs
                elif (result == 'pbo'):
                    if (bases[2]):
                        runs = runs + 1
                        bases[2] = bases[1]
                        bases[1] = bases[0]
                        bases[0] = 0
                        #max runs advance inning
                        if ((runs - start_runs)%MAX_RUNS_PER_INNING == 0):
                            df.loc[sim_runs*MAX_INNINGS + inning] = (inning, runs, outs, runs - start_runs)
                            inning = inning + 1
                            bases = [0,0,0]
                            outs = 0
                            start_runs = runs
                    else:
                        bases[2] = bases[1]
                        bases[1] = bases[0]
                        bases[0] = 0
            elif (result == 'p1b'):
                if (bases[2]):
                    runs = runs + 1
                    bases[2] = bases[1]
                    bases[1] = bases[0]
                    bases[0] = 1
                    #max runs advance inning
                    if ((runs - start_runs)%MAX_RUNS_PER_INNING == 0):
                        df.loc[sim_runs*MAX_INNINGS + inning] = (inning, runs, outs, runs - start_runs)
                        inning = inning + 1
                        bases = [0,0,0]
                        outs = 0
                        start_runs = runs
                else:
                    bases[2] = bases[1]
                    bases[1] = bases[0]
                    bases[0] = 1
            elif (result == 'p2b'):
                if (bases[2] or bases[1]):
                    runs = min(runs + bases[2] + bases[1],start_runs + MAX_RUNS_PER_INNING)
                    bases[2] = bases[0]
                    bases[1] = 1
                    bases[0] = 0
                    #max runs advance inning
                    if ((runs - start_runs)%MAX_RUNS_PER_INNING == 0):
                        df.loc[sim_runs*MAX_INNINGS + inning] = (inning, runs, outs, runs - start_runs)
                        inning = inning + 1
                        bases = [0,0,0]
                        outs = 0
                        start_runs = runs
                else:
                    bases[2] = bases[0]
                    bases[1] = 1
                    bases[0] = 0
            elif (result == 'p3b'):
                if (bases[2] or bases[1] or bases[0]):
                    runs = min(runs + bases[2] + bases[1] + bases[0],start_runs + MAX_RUNS_PER_INNING)
                    bases[2] = 1
                    bases[1] = 0
                    bases[0] = 0
                    #max runs advance inning
                    if ((runs - start_runs)%MAX_RUNS_PER_INNING == 0):
                        df.loc[sim_runs*MAX_INNINGS + inning] = (inning, runs, outs, runs - start_runs)
                        inning = inning + 1
                        bases = [0,0,0]
                        outs = 0
                        start_runs = runs
                else:
                    bases[2] = 1
                    bases[1] = 0
                    bases[0] = 0
            elif (result == 'phr'):
                runs = min(runs + bases[2] + bases[1] + bases[0] + 1,start_runs + MAX_RUNS_PER_INNING)
                bases[2] = 0
                bases[1] = 0
                bases[0] = 0
                #max runs advance inning
                if ((runs - start_runs)%MAX_RUNS_PER_INNING == 0):
                    df.loc[sim_runs*MAX_INNINGS + inning] = (inning, runs, outs, runs - start_runs)
                    inning = inning + 1
                    bases = [0,0,0]
                    outs = 0
                    start_runs = runs
    return df

#Standalone runner to get a min
def run_player_scenario_mc(all_players, order):
    df = run_player_scenario_mc_standalone(all_players,order)
    average_total_runs = df[df.Inning == MAX_INNINGS]['Runs'].sum()/df[df.Inning == MAX_INNINGS]['Runs'].count()
    #return max runs - average_total since it is a cost not a score
    return MAX_INNINGS*MAX_RUNS_PER_INNING - average_total_runs

def test():
    players =[Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0])]
    w_sum_mh = run_player_scenario_mh(players, [0,1,2,3,4,5,6,7,8,9])
    w_sum_mc = run_player_scenario_mc(players, [0,1,2,3,4,5,6,7,8,9])
    print(w_sum_mh,w_sum_mc)

    players =[Player([0.5, 0.0, 0.0, 0.0, 0.0, 0.5]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([0.5, 0.0, 0.0, 0.0, 0.0, 0.5]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([0.5, 0.0, 0.0, 0.0, 0.0, 0.5]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
          Player([1.0, 0.0, 0.0, 0.0, 0.0, 0.0])]
    w_sum_mh = run_player_scenario_mh(players, [0,1,2,3,4,5,6,7,8,9])
    w_sum_mc = run_player_scenario_mc(players, [0,1,2,3,4,5,6,7,8,9])
    print(w_sum_mh,w_sum_mc)

    w_sum_mh = run_player_scenario_mh(players, [0,1,2,3,4,5,6,7,8,9])
    w_sum_mc = run_player_scenario_mc(players, [0,1,2,3,4,5,6,7,8,9])
    print(w_sum_mh,w_sum_mc)


import cv2
import torch.cuda
from torchvision import transforms
from transformers import CLIPProcessor, CLIPModel
from tqdm.notebook import tqdm
from db_utils.database_handler import *

folder = "E:\\Yolo_v3_pretrained\\dataset\\videos\\"
# dictionary to map the movie title and the respective local filepath
movie_filepath_dict = {
    "iron man vs loki": folder + "Iron Man vs Loki - We have a Hulk - Suit Up Scene  "
                                 "The Avengers (2012) Movie Clip HD.mp4",
    "avengers age of ultron": folder + "Bruce Banner and Tony Stark put Jarvis Into "
                                       "Body - Avengers Age of Ultron (2015) Movie Clip HD Scene.mp4",
    "i love me": folder + "Demi Lovato - I Love Me (Official Video).mp4",
    "devil wears prada andy makeover": folder + "The Devil Wears Prada (45) Movie "
                                                "CLIP - Andy Gets a Makeover (2006) HD.mp4",
    "devil wears prada andy interview": folder + "The Devil Wears Prada (25) Movie "
                                                 "CLIP - Andys Interview (2006) HD.mp4",
    "no time to die": folder + "NO TIME TO DIE (2021)  Nuovo Trailer ITA del film con "
                               "007.mp4",
    "hot and cold": folder + "Katy Perry - Hot N Cold (Official).mp4",
    "inception": folder + "Inception - Ending.mp4",
    "the escape from limbo": folder + "The Escape from Limbo Inception ending Deja Vu.mp4"
}

def frame_to_tensor(frame: np.ndarray):
    transform = transforms.ToTensor()
    frame_t = transform(frame)
    return frame_t

def get_frame_patches(frame: np.ndarray, patch_size):
    """
    Function to split the frame into patches of size @patch_dim
    :param frame: the frame of the video
    :param patch_size: the dimension of the patches
    :return: the patches
    """
    frame_t = frame_to_tensor(frame)
    # unfold the tensor along the 0-dimension to get the batch dimension
    patches = frame_t.data.unfold(0, 3, 3)

    # create vertical patches (in the height dimension)
    patches = patches.unfold(1, patch_size, patch_size)

    # create horizontal patches (in width dimension)
    patches = patches.unfold(2, patch_size, patch_size)

    print(f"Shape of the patches = {patches.shape}")
    return patches

def load_model(model_id="openai/clip-vit-base-patch32"):
    """
    Function to load the transformer model and the respective preprocessor
    :param model_id: id of the model to load
    :return: the processor and the model requested
    """
    processor = CLIPProcessor.from_pretrained(model_id)
    model = CLIPModel.from_pretrained(model_id)
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model.to(device)
    return model, processor, device

def run_inference(model, processor, device, prompt, patches, patch_size, window, stride):
    """
    Function to run the model and get the similarity scores
    :param model: the Visual Transformer to be run
    :param processor: the processor associated to the Transformer to run
    :param device: the hardware devoted to run the model
    :param patches: the patches drawn from the frame
    :param patch_size: the size of the patches
    :param window: the amount of patches seen by the model
    :return: scores associated to the big patches
    """
    scores = torch.zeros(patches.shape[1], patches.shape[2])
    runs = torch.ones(patches.shape[1], patches.shape[2])

    for Y in range(0, patches.shape[1] - window + 1, stride):
        for X in range(0, patches.shape[2] - window + 1, stride):
            big_patch = torch.zeros(patch_size * window, patch_size * window, 3)
            patch_batch = patches[0, Y:Y + window, X:X + window]
            for y in range(window):
                for x in range(window):
                    big_patch[
                    y * patch_size:(y + 1) * patch_size, x * patch_size:(x + 1) * patch_size, :
                    ] = patch_batch[y, x].permute(1, 2, 0)
            # we preprocess the image and class label with the CLIP processor
            inputs = processor(
                images=big_patch,  # big patch image sent to CLIP
                return_tensors="pt",  # tell CLIP to return pytorch tensor
                text=prompt,  # class label sent to CLIP
                padding=True
            ).to(device)  # move to device if possible

            # calculate and retrieve similarity score
            score = model(**inputs).logits_per_image.item()
            # sum up similarity scores from current and previous big patches
            # that were calculated for patches within the current window
            scores[Y:Y + window, X:X + window] += score
            # calculate the number of runs on each patch within the current window
            runs[Y:Y + window, X:X + window] += 1
    # calculate average scores
    scores /= runs
    # clip scores
    for _ in range(3):
        scores = np.clip(scores - scores.mean(), 0, np.inf)
    # normalize scores
    scores = (scores - scores.min()) / (scores.max() - scores.min())
    return scores

def get_box(scores, patch_size, threshold):
    detection = scores > threshold
    # find box corners
    y_min, y_max = np.nonzero(detection)[:, 0].min().item(), np.nonzero(detection)[:, 0].max().item() + 1
    x_min, x_max = np.nonzero(detection)[:, 1].min().item(), np.nonzero(detection)[:, 1].max().item() + 1
    # convert from patch co-ords to pixel co-ords
    y_min *= patch_size
    y_max *= patch_size
    x_min *= patch_size
    x_max *= patch_size
    # calculate box height and width
    height = y_max - y_min
    width = x_max - x_min
    return x_min, y_min, width, height

def detect(model, processor, device, prompts, frame, patch_size=64, window=3, stride=1, threshold=0.5):
    """
    Function to the detect the objects in the frame. It uses the frames to look for the specified items.
    It creates a plot of the image containing the detected objects.
    :param model: model to run for the inference
    :param processor: processor associated to the model
    :param device: the hardware used to run the inference
    :param prompts: the objects to find in the frame
    :param frame: the specified frame
    :param patch_size: the size of the patches
    :param window: the amount of patches to search in simultaneously
    :return:
    """
    colors = ['#FAFF00', '#8CF1FF']
    # build image patches for detection
    frame_patches = get_frame_patches(frame, patch_size)
    frame_t = frame_to_tensor(frame)
    # convert image to format for displaying with matplotlib
    """
    image = np.moveaxis(frame_t.data.numpy(), 0, -1)
    X = frame_patches.shape[1]
    Y = frame_patches.shape[2]
    # initialize plot to display image + bounding boxes
    fig, ax = plt.subplots(figsize=(Y*0.5, X*0.5))
    ax.imshow(image)
    """
    # process image through object detection steps
    for i, prompt in enumerate(tqdm(prompts)):
        scores = run_inference(model, processor, device, prompt, frame_patches, patch_size, window, stride)
        x, y, width, height = get_box(scores, patch_size, threshold)
        # create the bounding box
        # rect = pltpatches.Rectangle((x, y), width, height, linewidth=3, edgecolor=colors[i], facecolor='none')
        cv2.rectangle(frame, (x, y), (x + width, y + height), [0, 255, 0])
        # add the patch to the Axes
        # ax.add_patch(rect)
    cv2.imshow("Frame", frame)

def show_video_and_detect(movie_title: str, movies_collection):
    url = movie_filepath_dict[movie_title.lower()]
    capture = cv2.VideoCapture(url)
    # For developing purpose, we are using the same fps as the detection algorithm
    # to retrieve the closest frame.
    fps = 0.1  # np.ceil(capture.get(cv2.CAP_PROP_FPS))
    # Till the end of the video
    while capture.isOpened():
        ret, frame = capture.read()

        if ret:
            cv2.imshow(movie_title, frame)
            # Press Q on keyboard to exit
            key = cv2.waitKey(25)
            if key & 0xFF == ord('q'):
                break
            elif key == 32:
                elapsed_seconds = capture.get(cv2.CAP_PROP_POS_MSEC) * 1e-3
                # debugging purpose
                # print("Elapsed seconds = {}".format(elapsed_seconds))
                elapsed_frames = int(np.floor(elapsed_seconds * fps))
                print("Elapsed frames = {}".format(elapsed_frames))
                t0 = time.time()
                # TODO Add database query
                bounding_boxes = get_frame_bounding_boxes(movies_collection, movie_title, elapsed_frames)
                print(f"Bounding boxes found = {bounding_boxes}")
                # TODO add boxes adjustments
                for bb in bounding_boxes:
                    cv2.rectangle(frame,  # frame
                                  (bb[0],  # x
                                   bb[1]),  # y
                                  (bb[0] + bb[2],  # width
                                   bb[1] + bb[3]),  # length
                                  [255, 0, 0]) # red box

                cv2.imshow(movie_title, frame)
                t1 = time.time()
                print("Time for retrieval = {%.3f}" % (t1 - t0))
                cv2.waitKey()
        else:
            break
    # When everything done, release
    # the video capture object
    capture.release()

    # Closes all the frames
    cv2.destroyAllWindows()

if __name__ == '__main__':
    """
    print("Type the title of the movie: ", end="")
    movie_title = input()
    print(movie_title)
    """
    client = db_connection('Piero_Rendina', 'R3nd1n@2021')
    movie_title = "Iron Man vs Loki"
    movies_info = client.movies.movies_info
    #get_detection_shape(movies_info, movie_title=movie_title)
    show_video_and_detect(movie_title, movies_info)


import random, re
import matplotlib.pyplot as plt

p = re.compile(r'^(\d{1,3}) (\d{1,3})$')
with open("locations.txt") as txt:
    coords = [[int(i) for i in p.search(l).groups()] for l in txt.readlines()]

res1 = [5,10,15,20,25,30,35,40]     # only visit 3 of these
res2 = [2,4,6,8,10,20,22,32,33,35]  # only visit 5 of these

class dataPreProcess():
    def __init__(self):
        self.data = coords
        self.ran_seq = random.sample(range(1,len(self.data)+1), len(self.data))
        self.res1 = res1
        self.res2 = res2

    def getInds(self):
        '''Select locations to visit and output their indices'''
        # visit at least 3 locations from res1, and 5 from res2
        res1_sel = random.sample(self.res1, 3)
        res2_sel = random.sample(self.res2, 5)

        # select unique locations
        res_set = set(res1).union(set(res2))
        res_sel_set = set(res1_sel).union(set(res2_sel))

        # remove all restricted locations, then add the selected ones
        fin_seq = [i for i in self.ran_seq if i not in res_set]
        fin_seq.extend(res_sel_set)
        return fin_seq

    def getLocs(self):
        ''' Output actual locations from data using selected locations '''
        # sort indices, then use them to find locations in data
        coords_indx_sort = sorted(self.getInds())
        coords_list = [self.data[i-1] for i in coords_indx_sort]

        # start and finish at the depot coordinate
        depot = [0,0]
        coords_list.insert(0, depot)
        coords_list.append(depot)

        return coords_list

    def plotPath(self, coords, path, N):
        xs = [coords[path[i]][0] for i in range(N+1)]
        ys = [coords[path[i]][1] for i in range(N+1)]
        plt.plot(xs, ys, 'ob-')
        plt.show()

    def plot2Path(self, coords, path1, path2, N):
        xs1 = [coords[path1[i]][0] for i in range(N+1)]
        ys1 = [coords[path1[i]][1] for i in range(N+1)]
        xs2 = [coords[path2[i]][0] for i in range(N+1)]
        ys2 = [coords[path2[i]][1] for i in range(N+1)]

        fig, axs = plt.subplots(1,2, figsize=(9.7, 5))
        fig.suptitle('Shortest paths by GA(left) and SA(right)')
        axs[0].plot(xs1, ys1, 'ob-')
        axs[1].plot(xs2, ys2, 'om-')
        plt.show()


import json
import simplejson
import os
import logging
import tempfile
import pandas as pd

from datetime import timedelta
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
from airflow.providers.google.cloud.hooks.gcs import GCSHook

class MergeHotelWeather(BaseOperator):
    @apply_defaults
    def __init__(
        self,
        gcp_conn_id,
        bucket_name,
        bucket_weather_subdir,
        bucket_hotel_subdir,
        *args,
        **kwargs,
    ):
        super(MergeHotelWeather, self).__init__(**kwargs)
        self._gcp_conn_id = gcp_conn_id
        self._bucket_name = bucket_name
        self._bucket_hotel_subdir = bucket_hotel_subdir
        self._bucket_weather_subdir = bucket_weather_subdir

    def execute(self, context):
        gcs_hook = GCSHook(self._gcp_conn_id)
        with tempfile.TemporaryDirectory() as tmp_dir:
            tmp_path_hotels = os.path.join(tmp_dir, "hotels.json")
            tmp_path_weather = os.path.join(tmp_dir, "weather.json")
            tmp_writer = os.path.join(tmp_dir, "hotels_weather.json")
            # download the hotels file from the bucket to the temporary directory
            gcs_hook.download(
                bucket_name=self._bucket_name,
                object_name=f"{self._bucket_hotel_subdir}/hotels_%s.json"
                % context["data_interval_end"].strftime("%Y-%m-%d"),
                filename=tmp_path_hotels,
            )
            # download the weather file fromt he bucket to the temporary directory
            gcs_hook.download(
                bucket_name=self._bucket_name,
                object_name=f"{self._bucket_weather_subdir}/weather_%s.json"
                % context["data_interval_end"].strftime("%Y-%m-%d"),
                filename=tmp_path_weather,
            )
            with open(tmp_path_hotels, "r", encoding="utf-8") as f_hotels, open(
                tmp_path_weather, "r", encoding="utf-8"
            ) as f_weather, open(tmp_writer, "w", encoding="utf-8") as f_tmp_writer:
                hotels = pd.DataFrame(json.load(f_hotels))
                weather = pd.DataFrame(json.load(f_weather))
                result = pd.merge(hotels,weather, how="left", on="dest_id")
                # We have to store the data as a Newline-Delimiter JSON (NDJSON) to ingest it into the Bigquery table
                for record in result.to_dict('records'):
                    f_tmp_writer.write(simplejson.dumps(record, ignore_nan=True)+'\n')
                gcs_hook.upload(
                    bucket_name=self._bucket_name,
                    object_name=f"results/hotel_weather.json",
                    filename=tmp_writer,
                )


import pprint

from pydantic import BaseModel, Field

class InputSchema(BaseModel):
    database_name: str = Field(
        title='Database Name',
        description='Name of the MongoDB database.'
    )
    collection_name: str = Field(
        title='Collection Name',
        description='Name of the MongoDB collection.'
    )
    filter: dict = Field(
        title='Filter Query',
        description='''
             A query document that selects which documents to count in the collection.
             Can be an empty document to count all documents.
             For eg: {"foo":"bar"}.
            '''
    )

def mongodb_count_documents_printer(output):
    if output is None:
        return
    if isinstance(output, int):
        pprint.pprint(f"Total number of documents : {output}")
    else:
        pprint.pprint(output)

def mongodb_count_documents(
        handle,
        database_name: str,
        collection_name: str,
        filter: dict
        ):
    """mongodb_count_documents Retrieves the documents present in
    the collection and the count of the documents using count_documents().

        :type handle: object
        :param handle: Object returned from task.validate(...).

        :type database_name: str
        :param database_name: Name of the MongoDB database.

        :type filter: Dict
        :param filter: A query that matches the document to filter.

        :rtype: All the results of the query.
    """
    # Input param validation.

    try:
        db = handle[database_name]
        total_count = db[collection_name].count_documents(filter)
        return total_count
    except Exception as e:
        return e


from flask import Flask
import utils

app = Flask(__name__)

"""Создайте представление для роута `/` (главная страница).

Выведите список в таком формате (тег
 - преформатирование)"""

@app.route("/")
def index():
    candidates = utils.get_all_candidates()
    result = '
'
    for candidate in candidates:
        result += candidate['name'] + "
"
        result += candidate['position'] + "
"
        result += candidate['skills'] + "
"
        result += '
'
    return f"
 {result}
"

"""Создайте представление для роута candidates/<x>"""

@app.route("/candidate/<int:pk>")
def get_candidates_by_pk(pk):
    candidate = utils.get_candidates_by_pk(pk)
    result = '
'
    result += candidate['name'] + "
"
    result += candidate['position'] + "
"
    result += candidate['skills'] + "
"
    result += '
'
    return f"""

 {result}

    """

"""Создайте представление `/skills/<x>` для поиска по навыкам.

Выведите тех кандидатов, в списке навыков у которых содержится `skill`.

Поиск по навыку не должен зависеть от регистра."""

@app.route("/candidate/<skill_name>")
def get_candidates_by_skill(skill_name):
    candidates = utils.get_candidates_by_skill(skill_name.lower())
    result = '
'
    for candidate in candidates:
        result += candidate['name'] + "
"
        result += candidate['position'] + "
"
        result += candidate['skills'] + "
"
        result += '
'
    return f"
 {result}
"

app.run(debug=True)


import numpy as np
import wave, array
import scipy.io as scio
import scipy.io.wavfile as wave1
from scipy import signal as si
import matplotlib.pyplot as plt

# a. Using experimentally measured HRIRs
# 1) Time domain
def loadHRIR1(database):
    """
    Load up Head Related Impulse Response database as in horizontal aspect for both ears.
    :param database: Provided Head Related Impulse Response database as .mat format.
    :return: Array of database.
    """
    # Load up the database
    HRIR = scio.loadmat(database)

    # Import two half circle models of the left ear
    left_1 = list(HRIR['hrir_l'][:, 8, :])
    left_2 = HRIR['hrir_l'][:, 40, :]

    # get left ear horizontal HRIR
    out_left = []
    for i in range(len(left_1) // 2 + 1, len(left_1)):
        out_left.append(left_1[i])
    left_a = []
    for i in range(len(left_2)):
        left_a.append(left_2[-i - 1])
    left_2 = left_a
    for i in range(len(left_2)):
        out_left.append(left_2[i])
    for i in range(len(left_1) // 2 + 1):
        out_left.append(left_1[i])

    # Import two half circle parts of the right ear
    right_1 = list(HRIR['hrir_r'][:, 8, :])
    right_2 = HRIR['hrir_r'][:, 40, :]

    # get right ear horizontal HRIR
    out_right = []
    for i in range(len(right_1) // 2 + 1, len(right_1)):
        out_right.append(right_1[i])
    right_a = []
    for i in range(len(right_2)):
        right_a.append(right_2[-i - 1])
    right_2 = right_a
    for i in range(len(right_2)):
        out_right.append(right_2[i])
    for i in range(len(right_1) // 2 + 1):
        out_right.append(right_1[i])

    # return the array of horizontal sound effect model for both two ears
    out_left = np.array(out_left)
    out_right = np.array(out_right)
    out = []
    for i in range(len(out_left)):
        out.append(out_left[i])
        out.append(out_right[i])
    out = np.array(out)
    return out

def loadHRIR2(database):
    """
    Load up Head Related Impulse Response database as in vertical aspect for both ears.
    :param database: Provided Head Related Impulse Response database as .mat format.
    :return: Array of database.
    """
    # Load up the database
    hrir = scio.loadmat(database)
    # import left vertical HRIR
    left_v = hrir['hrir_l'][:, 24, :]

    # the left HRIR function
    left_a = []
    for i in range(len(left_v)):
        left_a.append(left_v[-i - 1])
    out_left = left_a

    # import right vertical HRIR
    right_v = hrir['hrir_r'][:, 24, :]

    # the right HRIR function
    right_a = []
    for i in range(len(right_v)):
        right_a.append(right_v[-i - 1])
    out_right = right_a

    # the final HRIR function vertical model
    out_left = np.array(out_left)
    out_right = np.array(out_right)
    out = []
    for i in range(len(out_left)):
        out.append(out_left[i])
        out.append(out_right[i])
    out = np.array(out)
    return out

def loadWave(wav, hrir):
    """
    Load up the audio file and cut the wave into appropriate length snippets, the length of one snippets
    and the step length will be returned.
    :param wav: The audio file to be dealt with.
    :param hrir: Array of spatial sound model.
    :return signal: The list of audio data.
    :return points_number: The number of directions of the spatial sound effect.
    :return step: The length of each snippet to be dealt with one direction sound effect.
    """
    sfile = wave.open(wav, 'rb')
    (nchannels, sampwidth, fs, nf, comptype, compname) = sfile.getparams()

    # Compressed not supported yet
    assert comptype == 'NONE'
    array_type = {1: 'B', 2: 'h', 4: 'l'}[sampwidth]
    signal = list(array.array(array_type, sfile.readframes(nf))[::nchannels])

    # the 20 secs signal
    signal = np.array(signal)
    # length of signal
    length = len(signal)
    signal = signal.tolist()
    # number of directions
    points_number = len(hrir) // 2
    # m = ((length // points_number + 1) * points_number - length)
    # if length % points_number != 0:
    #     if m != 0:
    #         signal.append(0)  # zero padding
    #         m -= 1

    # length of signal in each direction
    step = length // points_number
    return signal, points_number, step

def Convolve(signal, hrir, step, points_number):
    """
    Use convolution function to deal with origin audio and spatial sound effect to achieve a stereo effect.
    :param signal: Original audio.
    :param hrir: Array of sound effect.
    :param step: Length of per snippet to be handled.
    :param points_number: The number of directions of the spatial sound effect.
    :return: Synthetic audio file of left channel and right channel.
    """
    # Convolution of two pieces
    segment_L = []
    segment_R = []
    signal = np.array(signal)
    for i in range(points_number):
        segment_L.append(np.convolve(hrir[2 * i], signal[step * i:step * (i + 1)], 'same'))
        segment_R.append(np.convolve(hrir[2 * i + 1], signal[step * i:step * (i + 1)], 'same'))
    return segment_L, segment_R

def Write(name, segment_L, segment_R):
    """
    Write the synthetic stereo effect audio data into the file with .wav format.
    :param name: The name of file.
    :param segment_L: Left channel data.
    :param segment_R: Right channel data.
    """
    # write 2D wave
    left = []
    right = []
    for item in segment_L:
        for sub in item:
            left.append(sub.real)
    for item in segment_R:
        for sub in item:
            right.append(sub.real)
    output = array.array('h', [])[::2]

    for i in range(len(left)):
        output.append(int(right[i] / 2))
        output.append(int(left[i] / 2))

    ofile = wave.open(name, 'w')
    ofile.setparams((2, 2, 44100, len(output), 'NONE', 'NONE'))
    ofile.writeframes(output.tobytes())

print('1) Time domain')

# horizontal
hrir_h = loadHRIR1('hrir_final.mat')
signal_h, points_number_h, step_h = loadWave('Mario.wav', hrir_h)
segment_L_h, segment_R_h = Convolve(signal_h, hrir_h, step_h, points_number_h)
Write("timeDomain_horizontal.wav", segment_L_h, segment_R_h)
print('Finished horizontal sound dealing.')

# vertical
hrir_v = loadHRIR2('hrir_final.mat')
signal_v, points_number_v, step_v = loadWave('Mario.wav', hrir_v)
segment_L_v, segment_R_v = Convolve(signal_v, hrir_v, step_v, points_number_v)
Write("timeDomain_vertical.wav", segment_L_v, segment_R_v)
print('Finished vertical sound dealing.')

# 2) Frequency domain
def freq(signal, hrir):
    """
    Use fast fourier transform to change the signal wave from time domain into frequency domain, then do a multiplication with HRIR
    to achieve stereo spatial sound effect.
    :param signal: Origin audio.
    :param hrir: Stereo sound effect model.
    :return: Left and right channel of audio
    """
    s = list(np.array(signal).T)  # signal
    for i in range(len(hrir)):
        s.append(0)
    s = np.array(s)

    number = 2 * (len(s) // len(hrir))  # the size of one piece
    s = s[:number // 2 * len(hrir)]
    x = []
    for i in range(len(hrir) // 2):
        x.append(s[i * number:(i + 1) * number])  # pieces of signal
    hrir = list(hrir)

    left_fft = []
    right_fft = []
    x_new = [[0] * 1 for i in range(len(x))]
    for i in range(len(hrir) // 2):
        x_new[i].pop(0)
        x_new[i] = np.fft.fft(x[i])
        hrir[2 * i] = np.fft.fft(hrir[2 * i], len(x_new[i]))
        hrir[2 * i + 1] = np.fft.fft(hrir[2 * i + 1], len(x_new[i]))
        left_fft.append(np.fft.ifft(np.multiply(x_new[i], hrir[2 * i])).real)  # total of multiply of left channel
        right_fft.append(np.fft.ifft(np.multiply(x_new[i], hrir[2 * i + 1])).real)  # total of multiply of right channel

    left = np.zeros(len(s) + 200 - 1)
    right = np.zeros(len(s) + 200 - 1)
    t = np.zeros(199)
    for i in range(len(left_fft)):
        left_fft[i][0:199] += t[0:199]
        t = left_fft[i][-199:]
        left[i * number:(i + 1) * number] = left_fft[i][0:number].real  # left channel fft
    t = np.zeros(199)
    for i in range(len(right_fft)):
        right_fft[i][0:199] += t[0:199]
        t = right_fft[i][-199:]
        right[i * number:(i + 1) * number] = right_fft[i][0:number].real  # right channel fft

    return left, right

def Write_f(name, left, right):
    """
    Write new audio into the file.
    :param name: File name.
    :param left: Left channel audio.
    :param right: Right channel audio.
    """
    output = array.array('h', [])[::2]
    for i in range(len(left)):
        output.append(int(right[i] / 2))
        output.append(int(left[i] / 2))
    # output = np.array(output).T
    ofile = wave.open(name, 'w')
    ofile.setparams((2, 2, 44100, len(output), 'NONE', 'NONE'))
    ofile.writeframes(output.tobytes())

print('\n2) Frequency domain')

left_h, right_h = freq(signal_h, hrir_h)
Write_f("freqDomain_horizontal.wav", left_h, right_h)
print('Horizontal sound of frequency domain have finished.')

left_v, right_v = freq(signal_v, hrir_v)
Write_f("freqDomain_vertical.wav", left_v, right_v)
print('Vertical sound of frequency domain have finished.')

# 3) Design part
def designHrir():
    """
    Customized spatial sound effect.
    :return:
    """
    t = 0.0001
    hrir_l = [[0] * 1 for i in range(36)]
    hrir_r = [[0] * 1 for i in range(36)]
    for theta in range(-90, 90, 5):
        hrir_r[(theta + 90) // 5].pop(0)
        hrir_l[(theta + 90) // 5].pop(0)
        for i in range(-50, 51, 1):
            w = 2 * i * np.pi * 441
            alpha = 0.5 * (1 + np.sin(theta / 180 * np.pi))
            Tr = (1 - alpha) * t
            Tl = alpha * t
            HR = (1 + 2j * alpha * w * t) / (1 + 1j * w * t) * np.exp(-1j * w * Tr)
            HL = (1 + 2j * (1 - alpha) * w * t) / (1 + 1j * w * t) * np.exp(-1j * w * Tl)
            hrir_r[(theta + 90) // 5].append(HR)
            hrir_l[(theta + 90) // 5].append(HL)

        hrir_r[(theta + 90) // 5] = hrir_r[(theta + 90) // 5][51:] + hrir_r[(theta + 90) // 5][:51]
        hrir_l[(theta + 90) // 5] = hrir_l[(theta + 90) // 5][51:] + hrir_l[(theta + 90) // 5][:51]
        # plt.plot(range(len(hrir_l[(theta + 90) // 5])), hrir_l[(theta + 90) // 5])
        # plt.plot(range(len(hrir_r[(theta + 90) // 5])), hrir_r[(theta + 90) // 5])
        # plt.show()
        hrir_r[(theta + 90) // 5] = list(np.fft.ifft(hrir_r[(theta + 90) // 5]).real)
        hrir_l[(theta + 90) // 5] = list(np.fft.ifft(hrir_l[(theta + 90) // 5]).real)

        hrir_r[(theta + 90) // 5] = hrir_r[(theta + 90) // 5][51:] + hrir_r[(theta + 90) // 5][:51]
        hrir_l[(theta + 90) // 5] = hrir_l[(theta + 90) // 5][51:] + hrir_l[(theta + 90) // 5][:51]
        # plt.plot(range(len(hrir_r[(theta + 90) // 5])), hrir_r[(theta + 90) // 5])
        # plt.plot(range(len(hrir_l[(theta+90)//5])), hrir_l[(theta+90)//5])
        # plt.show()
    hrir = []
    for i in range(len(hrir_r)):
        hrir.append(hrir_l[i])
        hrir.append(hrir_r[i])
    return hrir

print('\n3) Design part')
d_hrir = designHrir()
d_signal, d_points_number, d_step = loadWave("Mario.wav", d_hrir)
design_L, design_R = Convolve(d_signal, d_hrir, d_step, d_points_number)

Write("Design.wav", design_L, design_R)
print('Horizontal sound of design finished.\n')
print('All done.')


from typing import Optional

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        """
        Determines if a singly-linked list contains a cycle.
        Uses the fast and slow pointers technique.

        O(n) time, O(1) space.
        """
        if not head: return False
        p1 = head
        p2 = head
        while p2.next and p2.next.next:
            p1 = p1.next
            p2 = p2.next.next
            if p1 is p2: return True

        return False


# -*- coding: utf-8 -*-
"""
Created on Tue Oct 18 15:19:47 2022

@author: Matt Dodd
"""
import numpy as np
from array_funcs import *

def hex_count(len_d, elements=0):
    elements = len_d*6 + elements
    if len_d ==0:
        return elements
    len_d = len_d-1
    return hex_count(len_d,elements)

def rps(fmax,r,N):
    d0 = (3e8/fmax)/2
    if r >=1:
        xi = 1
    else:
        xi = 1/((N**r) - (N-1)**r)
    d= np.zeros(N+1)
    for n in range(N+1):
        d[n] = d0*xi*(n+1)**r

    return d

def rotate(xs,ys,theta,d):
    if len(theta) > len(d):
        print('Received more rotation angles than layers...')
        return
    theta = theta * np.pi / 180
    rotmat = np.array([[np.cos(theta), -1*np.sin(theta)],
                       [np.sin(theta), np.cos(theta)]])
    for n,t in enumerate(theta):
        b = hex_count(n) + 1
        e = hex_count(n+1) + 1
        [xs[b:e],ys[b:e]] = np.matmul(rotmat[:,:,n],[xs[b:e],ys[b:e]])
    return xs,ys

def hex_positions(d):
    xs = [None] * hex_count(len(d))
    ys = [None] * hex_count(len(d))
    for n,dn in enumerate(d):
        #dnsq = np.sqrt((dn**2)/2)
        dnsq = dn/2
        if n==0:
            xs[0] = dn
            ys[0] = 0
            xs[1] = dnsq
            ys[1] = dnsq
            xs[2] = -dnsq
            ys[2] = dnsq
            xs[3] = -dn
            ys[3] = 0
            xs[4] = -dnsq
            ys[4] = -dnsq
            xs[5] = dnsq
            ys[5] = -dnsq
        else:
            xs[hex_count(n)] = dn
            ys[hex_count(n)] = 0
            dn =  d[n]/(n+1)
            dnsq = d[n]/(n+1)/2
            for i in range(hex_count(n)+1,hex_count(n)+(n+2)):
                xs[i] = xs[i-1] - dnsq
                ys[i] = ys[i-1] + dnsq
            for i in range(hex_count(n)+(n+2),hex_count(n)+(2*n+3)):
                xs[i] = xs[i-1] - dn
                ys[i] = ys[i-1]
            for i in range(hex_count(n)+(2*n+3),hex_count(n)+(3*n+4)):
                xs[i] = xs[i-1] - dnsq
                ys[i] = ys[i-1] - dnsq
            for i in range(hex_count(n)+(3*n+4),hex_count(n)+(4*n+5)):
                xs[i] = xs[i-1] + dnsq
                ys[i] = ys[i-1] - dnsq
            for i in range(hex_count(n)+(4*n+5),hex_count(n)+(5*n+6)):
                xs[i] = xs[i-1] + dn
                ys[i] = ys[i-1]
            for i in range(hex_count(n)+(5*n+6),hex_count(n+1)):
                xs[i] = xs[i-1] + dnsq
                ys[i] = ys[i-1] + dnsq

    xs = np.insert(xs,0,0)
    ys = np.insert(ys,0,0)
    return xs,ys
if __name__ == '__main__':
    import matplotlib.pyplot as plt
    d= rps(3e9,1.8,3)
    xs, ys = hex_positions(d)
    rs = np.array([2,-10.3,-1.58,12.58])
    #xs,ys = rotate(xs,ys,rs,d)
    plt.scatter(xs,ys)
    plt.grid()
    ax = plt.gca()
    ax.set_aspect('equal','box')
    plt.show()


from collections import namedtuple

heart_rate_measurement_characteristic_id = '00002a37-0000-1000-8000-00805f9b34fb'

HeartRateMeasurement = namedtuple('HeartRateMeasurement', ['sensor_contact', 'bpm', 'rr_interval', 'energy_expended'])

def _parse_hr_measurement(data):
    flags = data[0]

    is_uint16_measurement_mask = 0x01
    is_contact_detected_mask = 0x06
    is_energy_expended_present_mask = 0x08
    is_rr_interval_present_mask = 0x10

    sensor_contact = None
    bpm = None
    rr_interval = []
    energy_expended = None

    measurement_byte_offset = 1
    sensor_contact = bool(flags & is_contact_detected_mask)

    if flags & is_uint16_measurement_mask:
        bpm = int.from_bytes(data[measurement_byte_offset:measurement_byte_offset + 2], 'little')
        measurement_byte_offset += 2
    else:
        bpm = data[measurement_byte_offset]
        measurement_byte_offset += 1

    if flags & is_energy_expended_present_mask:
        energy_expended = int.from_bytes(data[measurement_byte_offset:measurement_byte_offset + 2], 'little')
        measurement_byte_offset += 2

    if flags & is_rr_interval_present_mask:
        while len(data[measurement_byte_offset:]) >= 2:
            rr_interval.append(int.from_bytes(data[measurement_byte_offset:measurement_byte_offset + 2], 'little'))
            measurement_byte_offset += 2

    return HeartRateMeasurement(sensor_contact=sensor_contact,
                                bpm=bpm,
                                rr_interval=rr_interval,
                                energy_expended=energy_expended)

class HeartRateService:
    def __init__(self, client):
        self._client = client
        self._hr_measurement_callback = None

    async def enable_hr_measurement_notifications(self):
        await self._client.start_notify(heart_rate_measurement_characteristic_id,
                                        self._hr_measurement_notification_handler)

    async def disable_hr_measurement_notifications(self):
        await self._client.stop_notify(heart_rate_measurement_characteristic_id)

    def set_hr_measurement_handler(self, callback):
        self._hr_measurement_callback = callback

    def _hr_measurement_notification_handler(self, sender, data):  # pylint: disable=unused-argument
        if self._hr_measurement_callback is not None:
            self._hr_measurement_callback(_parse_hr_measurement(data))


import argparse
import logging
import re
from requests.exceptions import HTTPError
from urllib3.exceptions import MaxRetryError
import datetime
import csv
from common import config

import news_page_object as news
logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

#building regExp
is_well_formed_Link = re.compile(r'^https?://.+/.+$')#https://example.com/hello
is_root_path = re.compile(r'^/.+$')  #  /sometex123

def _news_scraper(news_site_uid, num='0'):
    num = int(num)
    ''' returns a list with data'''
    host = config()['news_sites'][news_site_uid]['url']
    logging.info('Beginning scraper for {}'.format(host))

    homepage = news.HomePage(news_site_uid,  host)
    Larticles = []
    if num != None: logger.warning('just {} articles will be fetched'.format(num))
    for link in homepage.article_links:
        article = _fetch_article(news_site_uid,  host,  link)

        if article:
            logger.info('Article fetched!')
            Larticles.append(article)
            if num == 0:
                continue
            elif len(Larticles)>num:
                break

    return Larticles

def _fetch_article(news_site_uid,  host,  link):
    '''method to fetch one article passed  '''
    logger.info('start fetching article at {}'.format(link))

    article = None
    try:
        article = news.ArticlePage(news_site_uid,  _build_link(host,  link))
    except (HTTPError, MaxRetryError) as e:
        logger.warning('Error while fetching the article',  exc_info = False)

    if article and not article.body :
        logger.warning('Invalid article. There is no body ')
        return None

    return article

def _build_link(host,  link):
    '''return string with url '''
    if is_well_formed_Link.match(link):
        return link
    elif is_root_path.match(link):
        return '{}{}'.format(host, link)
    else:
        return '{host}/{uri}'.format(host=host, uri=link)

def _save_articles(news_site_uid,  articles):
    ''' method for save tha data inaa csv file; no return'''
    #now = datetime.datetime.now().strftime('%Y_%m_%d')
    out_file_name = '../csv_files/{news_site_uid}.csv'.format(news_site_uid=news_site_uid)
    csv_headers = list(filter(lambda property: not property.startswith('_'),dir(articles[0]) ))

    with open(out_file_name,  mode='w+', encoding='utf-8') as f:
        writer = csv.writer(f, delimiter='|', quoting=csv.QUOTE_ALL , )
        writer.writerow(csv_headers)

        for article in articles:
            row = [str(getattr(article, prop)) for prop in csv_headers]
            writer.writerow(row)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    news_site_choices = list(config()['news_sites'].keys())
    parser.add_argument('news_site',
            help='the news site you want to scrape',
            type=str,
            choices= news_site_choices)
    parser.add_argument('int', type=str, nargs='?')

    args = parser.parse_args()

    # gets the data from the url
    data = _news_scraper(args.news_site, args.int)

    #creat the csv file from the data
    _save_articles(args.news_site,  articles = data)


 #coding=utf-8
import tensorflow as tf
import json
import re
import numpy as np
import random
from sklearn.metrics import r2_score
from scipy.stats import kendalltau
from sklearn import ensemble
import joblib

#邻接矩阵填充
def padding_zero_in_matrix(adjecent_matrix, module_operations):
    len_operations = len(module_operations)
    if len_operations != 7:
        # if the operations is less than 7
        for j in range(len_operations, 7):
            module_operations.insert(j - 1, 3)
        # print(important_metrics[i]['fixed_metrics']['module_operations'])

        padding_matrix = np.insert(adjecent_matrix, len_operations - 1,
                                       np.zeros([7 - len_operations, len_operations]), axis=0)
        padding_matrix = np.insert(padding_matrix, [len_operations - 1], np.zeros([7, 7 - len_operations]), axis=1)
    else:
        padding_matrix = np.array(adjecent_matrix)
    return padding_matrix, module_operations

#编码方法
def encode(adjecent_matrix, module_operations):
    #填充架构为相同大小的邻接矩阵和相同长度的节点标签列表
    padding_matrix, module_operations = padding_zero_in_matrix(adjecent_matrix, module_operations)
    #去除节点标签列表的in节点和out节点
    module_operations.remove(-1)
    module_operations.remove(-2)
    #将节点标签列表变成one-hot矩阵
    operations = []
    for i in range(0,len(module_operations)):
        ops = [ 0 for i in range(0,5)]
        if i < 3:
            ops[i] = 1
            operations.append(ops)
        else:
            operations.append(ops)
    operations = np.array(operations)
    #将邻接矩阵和节点标签列表化为一维并连接
    adj = list(np.ndarray.flatten(padding_matrix))
    operation = list(np.ndarray.flatten(operations))
    code = np.append(adj,operation)
    return code

#数据加载
def dataset_load(data_dir="data/data.json", graph_dir="data/generate_graphs.json", train_num=2500, test_num=500):
    #加载数据集中的key及对应的评估信息。
    model_id_regex="^"
    regex = re.compile(model_id_regex)
    with tf.gfile.Open(data_dir) as f:
        exist_models = json.load(f)
    exist_keys = [key for key in exist_models.keys() if regex.match(key)]
    random.shuffle(exist_keys)

    #分配训练集的key和测试集的key
    train_keys = exist_keys[0:train_num]
    test_keys = exist_keys[train_num:train_num+test_num]

    #获得key对应的架构的邻接矩阵和节点标签
    with tf.gfile.Open(graph_dir) as f:
        all_models = json.load(f)

    #分配训练集的邻接矩阵和节点标签以及标签（即性能）
    train_matrix = []
    train_ops = []
    train_label = []
    for i in range(len(train_keys)):
        key = train_keys[i]
        acc = exist_models[key]['evaluation_results'][2]['validation_accuracy']
        matrix = all_models[key][0]
        ops = all_models[key][1]
        train_matrix.append(matrix)
        train_ops.append(ops)
        train_label.append(acc)

    #分配测试集的邻接矩阵和节点标签以及标签（即性能）
    test_matrix = []
    test_ops = []
    test_label = []
    for i in range(len(test_keys)):
        key = test_keys[i]
        acc = exist_models[key]['evaluation_results'][2]['test_accuracy']
        matrix = all_models[key][0]
        ops = all_models[key][1]
        test_matrix.append(matrix)
        test_ops.append(ops)
        test_label.append(acc)
    return train_matrix, train_ops, train_label, test_matrix, test_ops, test_label

#获取模型
def get_model():
    model_random_forest_regressor = ensemble.RandomForestRegressor(n_estimators=230,max_features='auto')
    return model_random_forest_regressor

#数据编码
def encode_data(train_matrix, train_ops, train_label, test_matrix, test_ops, test_label):
    x_train = []
    for i in range(0,len(train_matrix)):
        x_train.append(encode(train_matrix[i],train_ops[i]))
    x_test = []
    for i in range(0,len(test_matrix)):
        x_test.append(encode(test_matrix[i],test_ops[i]))
    return x_train, train_label, x_test, test_label

#训练模型
def try_model(x_train, y_train, x_test, y_test, model):
    model.fit(x_train, y_train)
    result = model.predict(x_test)
    result = list(result)
    score = r2_score(y_test, result)
    result_arg = np.argsort(result)
    y_test_arg = np.argsort(y_test)
    result_rank = np.zeros(len(y_test_arg))
    y_test_rank = np.zeros(len(y_test_arg))
    for i in range(len(y_test_arg)):
        result_rank[result_arg[i]] = i
        y_test_rank[y_test_arg[i]] = i
    KTau, _ = kendalltau(result_rank, y_test_rank)
    joblib.dump(model, "output/RF_model.m") #save model
    print('KTau: {:}, R2score: {:}'.format(KTau, score))

if __name__ == '__main__':
    data_dir = "data/data.json"
    graph_dir = "data/generated_graphs.json"
    train_matrix, train_ops, train_label, test_matrix, test_ops, test_label = dataset_load(data_dir, graph_dir, 2000, 500)
    x_train, y_train, x_test, y_test = encode_data(train_matrix, train_ops, train_label, test_matrix, test_ops, test_label)
    model = get_model()
    try_model(x_train, y_train, x_test, y_test, model)


from __future__ import division, unicode_literals, print_function, absolute_import

import time

from .. import constants
from .resource import Resource
from .messagebased import MessageBasedResource
from . import helpers as hlp

class _GPIBMixin(object):
    """Common attributes and methods of GPIB Instr and Interface.
    """

    allow_dma = hlp.boolean_attr('VI_ATTR_DMA_ALLOW_EN',
                                 doc='This attribute specifies whether I/O accesses should '
                                     'use DMA (True) or Programmed I/O (False).')

    primary_address = hlp.range_attr('VI_ATTR_GPIB_PRIMARY_ADDR', 0, 30,
                                     doc='Specifies the primary address of the GPIB device used by the given session.',
                                     ro=True)

    remote_enabled = hlp.enum_attr('VI_ATTR_GPIB_REN_STATE', constants.LineState,
                                   doc='Returns the current state of the GPIB REN (Remote ENable) interface line.',
                                   ro=True)

    secondary_address = hlp.range_attr('VI_ATTR_GPIB_SECONDARY_ADDR', 0, 65535,
                                       doc='Specifies the secondary address of the GPIB device used by the given session.',
                                       ro=True)

    def assert_trigger(self):
        """Sends a software trigger to the device.
        """

        self.visalib.assert_trigger(self.session, constants.VI_TRIG_PROT_DEFAULT)

    def send_command(self, data):
        """Write GPIB command bytes on the bus.

        Corresponds to viGpibCommand function of the VISA library.

        :param data: data tor write.
        :type data: bytes
        :return: Number of written bytes, return value of the library call.
        :rtype: int, VISAStatus
        """
        return self.visalib.gpib_command(self.session, data)

    def control_atn(self, mode):
        """Specifies the state of the ATN line and the local active controller state.

        Corresponds to viGpibControlATN function of the VISA library.

        :param mode: Specifies the state of the ATN line and optionally the local active controller state.
                     (Constants.GPIB_ATN*)
        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_control_atn(self.session, mode)

    def control_ren(self, mode):
        """Controls the state of the GPIB Remote Enable (REN) interface line, and optionally the remote/local
        state of the device.

        Corresponds to viGpibControlREN function of the VISA library.

        :param mode: Specifies the state of the REN line and optionally the device remote/local state.
                     (Constants.GPIB_REN*)
        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_control_ren(self.session, mode)

    def pass_control(self, primary_address, secondary_address):
        """Tell the GPIB device at the specified address to become controller in charge (CIC).

        Corresponds to viGpibPassControl function of the VISA library.

        :param primary_address: Primary address of the GPIB device to which you want to pass control.
        :param secondary_address: Secondary address of the targeted GPIB device.
                                  If the targeted device does not have a secondary address,
                                  this parameter should contain the value Constants.NO_SEC_ADDR.
        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_pass_control(self.session, primary_address, secondary_address)

    def send_ifc(self):
        """Pulse the interface clear line (IFC) for at least 100 microseconds.

        Corresponds to viGpibSendIFC function of the VISA library.

        :return: return value of the library call.
        :rtype: VISAStatus
        """
        return self.visalib.gpib_send_ifc(self.session)

@Resource.register(constants.InterfaceType.gpib, 'INSTR')
class GPIBInstrument(_GPIBMixin, MessageBasedResource):
    """Communicates with to devices of type GPIB::<primary address>[::INSTR]

    More complex resource names can be specified with the following grammar:
        GPIB[board]::primary address[::secondary address][::INSTR]

    Do not instantiate directly, use :meth:`pyvisa.highlevel.ResourceManager.open_resource`.
    """

    def before_close(self):
        self.__switch_events_off()
        super(GPIBInstrument, self).before_close()

    def __switch_events_off(self):
        self.visalib.disable_event(self.session, constants.VI_ALL_ENABLED_EVENTS, constants.VI_ALL_MECH)
        self.visalib.discard_events(self.session, constants.VI_ALL_ENABLED_EVENTS, constants.VI_ALL_MECH)

    enable_repeat_addressing = hlp.boolean_attr('VI_ATTR_GPIB_READDR_EN',
                                                doc='Specifies whether to use repeat addressing '
                                                    'before each read or write operation.')

    enable_unaddressing = hlp.boolean_attr('VI_ATTR_GPIB_UNADDR_EN',
                                           doc='Specifies whether to unaddress the device (UNT and UNL) '
                                               'after each read or write operation.')

    def wait_for_srq(self, timeout=25000):
        """Wait for a serial request (SRQ) coming from the instrument.

        Note that this method is not ended when *another* instrument signals an
        SRQ, only *this* instrument.

        :param timeout: the maximum waiting time in milliseconds.
                        Defaul: 25000 (seconds).
                        None means waiting forever if necessary.
        """
        lib = self.visalib

        lib.enable_event(self.session, constants.VI_EVENT_SERVICE_REQ, constants.VI_QUEUE)

        if timeout and not(0 <= timeout <= 4294967295):
            raise ValueError("timeout value is invalid")

        starting_time = time.clock()

        while True:
            if timeout is None:
                adjusted_timeout = constants.VI_TMO_INFINITE
            else:
                adjusted_timeout = int((starting_time + timeout - time.clock()))
                if adjusted_timeout < 0:
                    adjusted_timeout = 0

            event_type, context, error = lib.wait_on_event(self.session, constants.VI_EVENT_SERVICE_REQ,
                                                           adjusted_timeout)
            lib.close(context)
            if self.stb & 0x40:
                break

        lib.discard_events(self.session, constants.VI_EVENT_SERVICE_REQ, constants.VI_QUEUE)

@Resource.register(constants.InterfaceType.gpib, 'INTFC')
class GPIBInterface(_GPIBMixin, Resource):
    """Communicates with to devices of type GPIB::INTFC

    More complex resource names can be specified with the following grammar:
        GPIB[board]::INTFC

    Do not instantiate directly, use :meth:`pyvisa.highlevel.ResourceManager.open_resource`.
    """

    address_state = hlp.enum_attr('VI_ATTR_GPIB_ADDR_STATE', constants.AddressState,
                                  'Shows whether the specified GPIB interface is currently addressed to talk or listen, or is not addressed.',
                                  ro=True)

    atn_state = hlp.enum_attr('VI_ATTR_GPIB_ATN_STATE', constants.LineState,
                              'Shows the current state of the GPIB ATN (ATtentioN) interface line.',
                              ro=True)

    is_controller_in_charge = hlp.boolean_attr('VI_ATTR_GPIB_CIC_STATE',
                                               'Shows whether the specified GPIB interface is currently CIC (Controller In Charge).',
                                               ro=True)

    is_system_controller = hlp.boolean_attr('VI_ATTR_GPIB_SYS_CNTRL_STATE',
                                            'Shows whether the specified GPIB interface is currently the system controller.\n\n'
                                            'In some implementations, this attribute may be modified only through a configuration utility.',
                                            ro=True)
    ndac_state = hlp.enum_attr('VI_ATTR_GPIB_NDAC_STATE', constants.LineState,
                               'Shows the current state of the GPIB NDAC (Not Data ACcepted) interface line.',
                               ro=True)

    def group_execute_trigger(self, *resources):

        for resource in resources:
            if not isinstance(resource, GPIBInstrument):
                raise ValueError('%r is not a GPIBInstrument', resource)

            # TODO: check that all resources are in the same board.

        if not self.is_controller_in_charge:
            self.send_ifc()

        command = [0x40, 0x20+31, ] # broadcast TAD#0 and "UNL" (don't listen) to all devices

        for resource in resources:
            # tell device GPIB::11 to listen
            command.append(0x20 + resource.primary_address)

        # send GET ('group execute trigger')
        command.append(0x08)

        return self.send_command(bytes(command))


import argparse
import random
import os

from PIL import Image
from tqdm import tqdm

SIZE = 128

parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', default='../data/dataset', help='Directory with the currency dataset')
parser.add_argument('--output_dir', default='../data/224x224_currency', help='where to write the new dataset')

def resize_and_save(filename, output_dir, size=SIZE):
    """Resize the image contianed in `filename` and save it to the `output_dir`"""
    image = Image.open(filename)
    # use bilinear interpolation instead of the default "nearest neighbor" method
    image = image.resize((size, size), Image.BILINEAR)
    image.save(os.path.join(output_dir, filename.split('/')[-1]))

if __name__ == '__main__':
    args = parser.parse_args()

    assert os.path.isdir(args.data_dir), "Couldn't find the dataset at {}".format(args.data_dir)

    # define the data directories
    train_data_dir = os.path.join(args.data_dir, 'train_dir')
    dev_data_dir = os.path.join(args.data_dir, 'dev_dir')
    test_data_dir = os.path.join(args.data_dir, 'test_dir')

    # Get the filenames in dataset directory
    filenames = os.listdir(args.data_dir)
    filenames = [os.path.join(args.data_dir, f) for f in filenames if f.endswith('.jpg')]

    # split the images in 'currenct' into 80% train, 10% dev 10% test
    # make sure to always shuffle with a fixed seed so that spllit is reproducible
    random.seed(230)
    filenames.sort()
    random.shuffle(filenames)

    split_1 = int(0.8 * len(filenames))
    split_2 = int(0.9 * len(filenames))
    train_filenames = filenames[:split_1]
    dev_filenames = filenames[split_1: split_2]
    test_filenames = filenames[split_2:]

    filenames = {'train': train_filenames,
                 'dev': dev_filenames,
                 'test': test_filenames}

    if not os.path.exists(args.output_dir):
        os.mkdir(args.output_dir)
    else:
        print('Warning: output dir {} already exists'.format(args.output_dir))

    # Preprocess train, dev and test
    for split in ['train', 'dev', 'test']:
        output_dir_split = os.path.join(args.output_dir, '{}_dir'.format(split))
        if not os.path.exists(output_dir_split):
            os.mkdir(output_dir_split)
        else:
            print('Warning: dir {} already exits'.format(output_dir_split))

        print('Processing {} data, saving preprocessed data to {}'.format(split, output_dir_split))
        for filename in tqdm(filenames[split]):
            resize_and_save(filename, output_dir_split, size=SIZE)

    print("Done building dataset")


import sys
import numpy as np

from bayes_opt import BayesianOptimization
from bayes_opt.util import UtilityFunction, Colours

from tools import write2tree2 as w
from tools.getdata import get_position, get_residuals
from tools.fitness import xyz_fit,residuals_fit

def black_box_function(wetz,dryz,Iu,Uu,PIu,PUu,Psigma):
    path='/home/WVU-AD/jgross2/optimizer/data'
    params=[wetz,dryz,Iu,Uu,PIu,PUu,Psigma]
    #write parameters to tree and check rtgx ran succesfully
    flag,path2run = w.write2tree(params, path)

    if flag != True:
        sys.exit() #if rtgx fails stop this program

    #Extract data
    x,y,z,Time = get_position(solution_idx=None,generation=None,path=path2run)
    phase, TimeP, range, TimeR = get_residuals(solution_idx=None,generation=None,path=path2run)

    #Get fitness from this data
    xyzfitness=xyz_fit(x,y,z,Time)
    residualfitness=residuals_fit(phase, TimeP, range, TimeR )

    fitness=xyzfitness+residualfitness

    return fitness
# wetz,dryz,Iu,Uu,PIu,PUu,Psigma
pbounds = { 'dryz': (1.5, 2.5),'wetz': (0.1, 0.25),'Iu':(0.1,1),'Uu': (0.01,0.3),'PIu':(5,1000),'PUu':(1,500),'Psigma':(0.01,0.3)}

optimizer = BayesianOptimization(
    f=black_box_function,
    pbounds=pbounds,
    random_state=1,
)

optimizer.maximize(n_iter=30)


import torch
import torch.nn as nn
import torch.nn.functional as func

from tools import GesdSim

class Simler(nn.Module):

    def __init__(self, negative_size, threshold):
        super(Simler, self).__init__()
        self.similarity = GesdSim(gamma=1.0, c=1.0)
        self.negative_size = negative_size
        self.threshold = threshold
        self.pool = torch.max

    def forward(self, q, pa, na):
        if self.training:
            return self._train(q, pa, na)
        else:
            return self._evaluate(q, pa, na)

    def _train(self, q, pa, na):
        pa_sim = self.similarity(q, pa)
        if not hasattr(torch.Tensor, 'repeat_interleave'):
            na_sim = torch.zeros(q.shape[0])
            if torch.cuda.is_available():
                na_sim = na_sim.cuda()
            for i in range(q.shape[0]):
                temp = na[i * self.negative_size: (i + 1) * self.negative_size]
                temp = self.similarity(q[i].repeat(self.negative_size, 1), temp)
                na_sim[i] = temp.max()
                del temp
        else:
            q_repeat = q.repeat_interleave(self.negative_size, 0)
            na_sim = self.similarity(q_repeat, na).view((-1, self.negative_size))
            na_sim = torch.max(na_sim, 1)[0]
        if torch.cuda.is_available():
            loss = (torch.ones(q.shape[0]) * self.threshold).cuda() - pa_sim + na_sim
        else:
            loss = torch.ones(q.shape[0]) * self.threshold - pa_sim + na_sim
        return loss.mean(), (pa_sim.mean() - na_sim.mean()) / 2

    def _evaluate(self, q, pa, na):
        loss, _ = self._train(q, pa, na)
        accu = 0
        pa_sim = self.similarity(q, pa)
        for i in range(q.shape[0]):
            temp = na[i * self.negative_size: (i + 1) * self.negative_size]
            temp_sim = self.similarity(q[i].repeat(self.negative_size, 1), temp)
            temp_rank = torch.zeros_like(temp_sim)
            temp_rank[temp_sim > pa_sim[i]] = 1
            accu += 1 / (temp_rank.sum().item() + 1)
        return loss.item(), accu / q.shape[0]


import numpy as np
import pandas as pd
import scipy.sparse as sp
import math
from sklearn.datasets import fetch_rcv1

def freq(x, prob=True):
    return np.unique(x), x.value_counts(prob)

def freq2(x, y, prob=True):
    df = pd.concat([x, y], axis=1)
    return np.unique(x), np.unique(y), df.value_counts(normalize=prob)

def entropy(x):
    suma = 0
    _, values = freq(x)
    for i in values:
        suma += i * math.log(i, 2)
    return -suma

def info_gain(x, y):
    suma = 0
    _, _, z = freq2(x, y)
    for i in z:
        suma += i * math.log(i, 2)
    return entropy(x) + entropy(y) - -suma

# Zad 5
def convert_to_sparse_pandas(DataFrame, exclude_columns=[]):
    DataFrame = DataFrame.copy()
    exclude_columns = set(exclude_columns)
    for (columnName, columnData) in DataFrame.iteritems():
        if columnName not in exclude_columns:
            DataFrame[columnName] = pd.SparseArray(columnData.values, fill_value=0, dtype='uint8')

    return DataFrame

if __name__ == '__main__':
    x = pd.read_csv("zoo.csv")
    print(freq(x['legs']))
    print(freq(x['legs'], prob=False))
    print("\n", freq2(x['legs'], x['type'], prob=True))
    print("\n", freq2(x['legs'], x['type'], prob=False))
    print(entropy(x['type']))
    print(info_gain(x['domestic'], x['type']))
    print(x.columns)
    print("\n\n")

    # Zad 4
    x = pd.read_csv("zoo.csv")
    for i in x.columns:
        if i != 'type':
            print(i, info_gain(x[i], x['type']))
    print("\n\n")

    # Zad 5
    data, numbers = freq(x['legs'])
    data_one_hot = pd.get_dummies(x, columns=x.columns)
    print(data_one_hot.head(), data_one_hot.dtypes)
    data_one_hot_sparse = convert_to_sparse_pandas(data_one_hot)
    print(data_one_hot_sparse.head(), data_one_hot_sparse.dtypes)
    print("\n\n")
    print(freq(data_one_hot_sparse['legs_4']))
    print(freq2(data_one_hot_sparse['eggs_True'], data_one_hot_sparse['type_bird'], prob=True))

    # Zad 6
    rcv1 = fetch_rcv1()
    df = pd.DataFrame(rcv1.data, columns=rcv1.target_names)
    print(df.head())


# 作用：获取本地的外网出口地址
import requests
import re

def getIP():
    headers = {}
    headers['User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
    response = requests.get('http://www.baidu.com/s?wd=ip', headers=headers)
    html = response.text
    # reGET = re.compile('fk="(.*?)"').findall(html)
    # reGET = re.compile('ip地址(.*?) ').findall(html)
    reGET = re.compile('ip地址(.*?)。').findall(html)
    for i in reGET:
        print('外网地址： %s' % i)
        return i

ipinfo = getIP()

def save(file, res):
    with open(file, 'w', encoding='utf-8') as f:
        f.write(res)

def 时间_到文本(时间格式index=0, timetuple=None):
    '''

    :param 时间格式index:
    # 0 :  N年N月N日  示例:2020年11月20日
    # 1 :  N年N月N日N时N分N秒  示例:2020年11月20日4时25分38秒
    # 2 :  N年N月N日 N时N分N秒  示例:2020年11月20日 4时25分38秒
    # 3 :  年-月-日 时:分:秒  2020-11-20 4:25:38
    # 4 :  年/月/日 时/分/秒  2020/11/20 4/25/38
    :param timetuple:
        # 可传入 时间元组  如果不传 就默认取现行时间
    :return:
    '''
    # Done (2020年11月20日) 在版本3基础上 添加功能:时间格式 如果后续有需要 直接去list中添加对应格式
    # 时间格式index= ?
    # 0 :  N年N月N日  示例:2020年11月20日
    # 1 :  N年N月N日N时N分N秒  示例:2020年11月20日4时25分38秒
    # 2 :  N年N月N日 N时N分N秒  示例:2020年11月20日 4时25分38秒
    # 3 :  年-月-日 时:分:秒  2020-11-20 4:25:38
    # 4 :  年/月/日 时/分/秒  2020/11/20 4/25/38

    # def 时间_到文本(包含时分秒=False, timetuple=None):
    # 在版本2基础上 添加功能:支持传入 时间元组  如果不传 就默认取现行时间
    # struct_time  一般由time.localtime([secs])生成 参数secs省略就是取现行时间戳time.time()
    # struct_time 被提示重名了 改成了 timetuple

    import time
    # 检查到默认时间元组为None,那么就是取现行时间      # 如果没传入时间元组 就代表取现行时间
    if timetuple is None:
        timetuple = time.localtime()
    # time.struct_time(tm_year=2019, tm_mon=9, tm_mday=16, tm_hour=23, tm_min=45, tm_sec=13, tm_wday=0, tm_yday=259, tm_isdst=0)

    # 用*打散列表/元组等有序合集即可,需要几个参数它自己会传入个数,不会报错
    # N年N月N日 N时N分N秒  1=年-月-日 时:分:秒  2=年/月/日 时/分/秒  3=年月日时分秒
    # 时间格式index
    # 0 :  N年N月N日  示例:2020年11月20日
    # 1 :  N年N月N日N时N分N秒  示例:2020年11月20日4时25分38秒
    # 2 :  N年N月N日 N时N分N秒  示例:2020年11月20日 4时25分38秒
    # 3 :  年-月-日 时:分:秒  2020-11-20 4:25:38
    # 4 :  年/月/日 时/分/秒  2020/11/20 4/25/38
    result_list = [
        "{}年{}月{}日",
        "{}年{}月{}日{}时{}分{}秒",
        "{}年{}月{}日 {}时{}分{}秒",
        "{}-{}-{} {}:{}:{}",
        "{}/{}/{} {}/{}/{}",
    ]

    return result_list[时间格式index].format(*timetuple)

file = 时间_到文本(时间格式index=1) + ".txt"
save(file, ipinfo)


#!/usr/bin/env python
# coding=utf8
from setuptools import setup, find_packages
import os

if os.path.exists('requirements.txt'):
    requirements = [x.strip() for x in open("requirements.txt").readlines()]
else:
    requirements = []

# Create build meta
setup(
    name="qcaudit",
    version="{PYPI_VERSION}",
    author="{CI_COMMIT_AUTHOR}",
    url="{DRONE_REMOTE_URL}",
    packages=find_packages(),
    include_package_data=True,
    install_requires=requirements,
    description="",
    long_description=open("README.md").read(),
)


from bayesian_optimization.plotter.plot import Plot
from bayesian_optimization.datahandler.data_table import COLUMNS
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

class RegretPlot(Plot):

    def __init__(self, figsize, title, out_path, df):
        super().__init__(figsize, title, out_path)
        self.df = df
        self.legend = None

    def with_axis_range(self, index:int, ylim, visible=True):
        self._add_twinx_if_needed(index)
        self.axis[index-1].set_ylim(ylim)
        self.axis[index-1].patch.set_alpha(0.1)
        if not visible:
            self.axis[index-1].tick_params(
                axis='y',
                which='both',
                bottom=False,
                top=False,
                left=False,
                right=False,
                labelright=False
            )
        return self

    def with_log_y(self):
        axs = plt.gca()
        axs.set(yscale="log")
        return self

    def set_y_lim(self, lim):
        axs = plt.gca()
        axs.set_ylim(lim)
        return self

    def draw_line_with_mean(self, ax=1, x=COLUMNS.STEP, y=COLUMNS.REGRET, hue=COLUMNS.METHODS, key_list=None, value_list=None, ci=None, linestyle="-", **kwargs):
        return self.draw_line(ax=ax, x=x, y=y, hue=hue, key_list=key_list, value_list=value_list, ci=ci, estimator=np.mean, linestyle=linestyle, **kwargs)

    def draw_line_with_median(self, ax=1, x=COLUMNS.STEP, y=COLUMNS.REGRET, hue=COLUMNS.METHODS, key_list=None, value_list=None, ci=None,linestyle="-", **kwargs):
        return self.draw_line(ax=ax, x=x, y=y, hue=hue, key_list=key_list, value_list=value_list, ci=ci, estimator=np.median, linestyle=linestyle, **kwargs)

    def draw_line(
            self,
            ax=1,
            x=COLUMNS.STEP,
            y=COLUMNS.REGRET,
            hue=COLUMNS.METHODS,
            key_list=None,
            value_list=None,
            ci=None,
            estimator=np.mean,
            linestyle="-",
            **kwargs
    ):
        if key_list is None:  #
            key_list = [COLUMNS.METHODS]
        if value_list is None:  #
            value_list = [("GP",), ("NOMU",), ("DE",), ("DO",)]
        data = self.df[self.df[key_list].apply(tuple, axis=1).isin(value_list)]
        dashes = [(1, 0) for i in data[hue].unique()]
        if linestyle == "-":
            dashes = [(1,0) for i in data[hue].unique()]
        if linestyle == "--":
            dashes = [(2,1) for i in data[hue].unique()]
        splot = sns.lineplot(data=data, x=x, y=y, hue=hue, estimator=estimator, ci=ci, dashes=dashes, style=hue, **kwargs)
        return self


# -*- coding: utf-8 -*-
"""
@File    : cdndomain_list.py
@Time    : 2023/6/30 12:01 下午
@Author  : xxlaila
@Software: PyCharm
"""

from django.db import models
from django.db.models import F
import logging
import uuid
from domains.models.domain_list import CLOUD_CHOICES

__all__ = ['CdnDomainsList']
logger = logging.getLogger(__name__)

READONLY_CHOICES = (
    ("normal", "未锁定"),
    ("mainland", "中国境内锁定"),
    ("overseas", "中国境外锁定"),
    ("global", "全球锁定")
)

AREA_CHOICES = (
    ("mainland", "中国境内加速"),
    ("overseas", "中国境外加速"),
    ("mainland_china", "中国大陆"),
    ("outside_mainland_china", "中国大陆境外"),
    ("global", "全球加速")
)

DISABLE_CHOICES = (
    ("normal", "正常状态"),
    ("overdue", "账号欠费导致域名关闭，充值完成后可自行启动加速服务"),
    ("malicious", "域名出现恶意行为，强制关闭加速服务"),
    ("ddos", "域名被大规模 DDoS 攻击，关闭加速服务"),
    ("idle", "域名超过 90 天内无任何操作、数据产生，判定为不活跃域名自动关闭加速服务，可自行启动加速服务"),
    ("unlicensed", "域名未备案 / 备案注销，自动关闭加速服务，备案完成后可自行启动加速服务"),
    ("capping", "触发配置的带宽阈值上限"),
    ("readonly", "域名存在特殊配置，被锁定"),
)

SERVICETYPE_CHOICES = (
    ("web", "静态加速"),
    ("download", "下载加速"),
    ("media", "流媒体点播加速"),
    ("hybrid", "动静加速"),
    ("dynamic", "动态加速"),
    ("video", "点播加速"),
    ("wholeSite", "全站加速")
)

STATUS_CHOICES = (
    ("rejected", "域名审核未通过，域名备案过期/被注销导致"),
    ("processing", "部署中"),
    ("configuring", "配置中"),
    ("configure_failed", "配置失败"),
    ("checking", "审核中"),
    ("check_failed", "审核未通过"),
    ("deleting", "删除中"),
    ("closing", "关闭中"),
    ("online", "已启动"),
    ("offline", "已关闭"),
)

class CdnDomainsList(models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    resourceid = models.CharField(null=True, blank=True, max_length=64, verbose_name="域名ID")
    domain = models.CharField(null=True, blank=True, max_length=256, db_index=True, verbose_name="加速域名")
    cname = models.CharField(null=True, blank=True, max_length=512, db_index=True, verbose_name="CNAME 地址")
    status = models.CharField(choices=STATUS_CHOICES, max_length=32, verbose_name="加速服务状态")
    servicetype = models.CharField(choices=SERVICETYPE_CHOICES, max_length=32, verbose_name="业务类型")
    origin = models.TextField(null=True, blank=True, verbose_name="源站配置详情")
    origins = models.TextField(null=True, blank=True, verbose_name="回源地址")
    disable = models.CharField(choices=DISABLE_CHOICES, max_length=32, verbose_name="封禁状态")
    area = models.CharField(choices=AREA_CHOICES, max_length=32, verbose_name="加速区域")
    readonly = models.CharField(choices=READONLY_CHOICES, max_length=32, verbose_name="锁定状态")
    product = models.CharField(null=True, blank=True, max_length=16, verbose_name="所属产品")
    parentHost = models.CharField(null=True, blank=True, max_length=128, verbose_name="主域名")
    cloud = models.CharField(choices=CLOUD_CHOICES, db_index=True, max_length=32, verbose_name='云')
    create_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, null=True, verbose_name="更新时间")

    class Meta:
        ordering = ['-domain']
        verbose_name = "CdnDomainsList"
        verbose_name_plural = "CdnDomainsList"


import unittest.mock
import datetime
from unittest.mock import MagicMock
from unittest.mock import patch
from clock.Clock import Clock
from clock.SpeakingClock import SpeakingClock

class TestSpeakingClock(unittest.TestCase):

    def test_speaking_clock_gets_time_from_clock(self):
        # arrange
        #  create the clock
        mock_clock = Clock()
        # mock the get_time method to always return midday
        mock_clock.get_time = MagicMock(return_value = datetime.datetime(2017,12,1,12,0,0))
        # create the speaking clock and then assign the mock_clock to be used instead of a normal clock
        speaking_clock = SpeakingClock()
        speaking_clock.clock = mock_clock
        # act
        # invoke the say_time method
        return_value = speaking_clock.say_time()
        # assert
        # check that the clock was asked what the time is
        # mock is acting as a spy
        mock_clock.get_time.assert_called_once()
        # check that the value returned from the speaking clock is actually 'midday' given that is the time
        self.assertEqual(return_value, 'midday')

    # This only works with Python 3.6.x. It does not work with Python 3.5.x
    def test_speaking_clock_gets_time_from_clock_with_patching(self):
        with patch.object(Clock, 'get_time', return_value=datetime.datetime(2017,12,1,12,0,0)) as mock_method:
            speaking_clock = SpeakingClock()
            return_value = speaking_clock.say_time()
            mock_method.assert_called_once()
            self.assertEqual(return_value, 'midday')

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


from tifffile import imread
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import (QDialog, QSizePolicy, QApplication, QTableWidget, QVBoxLayout,
                            QPushButton, QColorDialog, QTableWidgetItem, QMessageBox)
from PyQt5.QtGui import QCursor, QColor
from PyQt5.QtCore import Qt
from matplotlib.colors import LinearSegmentedColormap
import copy, re, sys
from ast import literal_eval
import subWindows as sw

def loadStacks5D(file_name, app=False):
    print('#'*40)
    print('Loading dataset at:\n\t', file_name)
    stacks = imread(file_name)
    target_id = 'TZCHW'

    # append dimensions if necessary
    if not app:
        app = QApplication(sys.argv)
    ddef = sw.DimensionDefiner(shape=stacks.shape)
    ddef.show()
    if ddef.exec_() == QDialog.Accepted:
        input_id = ddef.text
        # find the missing id
        missing_id = copy.deepcopy(target_id)
        for i in input_id:
            missing_id = missing_id.replace(i,'')
        # append dimension
        for i in range(len(target_id)-len(input_id)):
            stacks = np.expand_dims(stacks,-1)
            input_id = input_id+missing_id[i]
    else:
        return

    # reorder dimensions to match 'TZCHW'
    if input_id != target_id:
        for i in target_id:
            f = input_id.index(i)
            t = target_id.index(i)
            stacks = np.moveaxis(stacks,f,t)
            input_id = input_id.replace(i,'')
            input_id = input_id[:t]+i+input_id[t:]

    # adjust channel dimension to 2
    if stacks.shape[2]==1:
      stacks = np.concatenate([stacks,np.zeros(stacks.shape)],axis=2)

    # compute maximum values
    _maxval = np.zeros((stacks.shape[0],stacks.shape[2]))
    for i in range(stacks.shape[0]):
        for j in range(stacks.shape[2]):
            _maxval[i,j] = np.max(stacks[i,:,j,:,:])
    print('Stack shape (TZCHW):', stacks.shape)
    print('Done')
    return stacks, _maxval

def swapAxes(stacks, _maxval, ax = 0):
    stacks = np.flip( stacks, axis = ax )
    _maxval = np.flip(_maxval,axis = ax-2)
    return stacks, _maxval

def convertPoints(oldData):
    new = {'_ids': ['tether_Atrium','tether_Ventricle','AVCanal','Midline'],
                        'colors': ['#6eadd8','#ff7f0e','red','#c4c4c4'],
                        'markers': ['o','o','X','-x'],
                        'ms': [3,3,5,1],
                        'is_instance': [0,0,0,0],
                        'coords': [np.array([])]*4 }
    for key in oldData:
        i = new['_ids'].index(key)
        new['coords'][i] = oldData[key]
        if oldData[key].shape[0]>0:
            new['is_instance'][i] = 1
    return new

if __name__ == '__main__':

    f = '../test_unwrap_heart/4D_merged_small.tif'
    loadStacks5D(f)


"""
Will print whether or not there is a dog in the input image file.
"""
import argparse
from dog_breed.detectors import detectors

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Will print whether or not there is a dog in the input image file.')

    parser.add_argument('-f', '--file', help='Path of the image', required=True)
    args = parser.parse_args()
    dog = detectors.is_dog(args.file)
    if dog:
        print("The image represents a dog")
    else:
        print("No dog detected in the image")


"""
Tools for shuffling a time interval data set,
i.e. where we know the start and end times and wish to know which months it contains.
"""
import logging
import pandas as pd

from .utilities import shuffle
from .progress_bar import ProgressBar
from .file_structure import START, END

class GetMonth:
    """
    Iterate through a data set from 'start' to 'end' by month,
    extracting values that overlap that time, and shuffle values.
    Parameters
    ----------
    start: datetime.datetime
        date stamp to start from
    end: datetime.datetime
        date time to end at.
    """
    def __init__(self, start=START, end=END):
        self.start = start
        self.end = end

    @property
    def months(self):
        """Month start date(s) between 'start' and 'end'"""
        return pd.date_range(self.start, self.end, freq="MS")

    @property
    def current_months(self):
        """Start of monthly periods"""
        return self.months[:-1]

    @property
    def next_month(self):
        """End of monthly periods"""
        return self.months[1:]

    def yield_data(self, data, start_column="Absence Start Date", end_column="Absence End Date"):
        """
        Iterate through data, looking up months bound by start_column and end_column.
        Parameters
        ----------
        data: pandas.DataFrame
            frame to look up in
        start_column: str
            the column of data that serves as month start
        end_column: str
            the column of data the serves as month end
        """
        zipped_months = zip(self.current_months, self.next_month)

        data[start_column] = pd.to_datetime(data[start_column])
        data[end_column] = pd.to_datetime(data[end_column])

        for start_of_month, end_of_month in zipped_months:
            month = data[
                (data[start_column] < end_of_month) &
                (data[end_column] >= start_of_month)
            ]
            month["Month"] = start_of_month
            yield start_of_month, month

    def monthly_shuffle(self, data, columns_to_shuffle,
                        start_column="Absence Start Date", end_column="Absence End Date"):
        """
        Take a data set, extract a month, and shuffle.
        Parameters
        ----------
        data: pandas.DataFrame
            the data set to monthly shuffle
        columns_to_shuffle: str
            the column of 'data' to shuffle
        start_column: str
            the column to treat as month start
        end_column: str
            the column to treat as month end
        """
        monthly_data = self.yield_data(data, start_column, end_column)
        for start_of_month, month in monthly_data:
            month[columns_to_shuffle] = shuffle(
                frm=month[columns_to_shuffle],
                name=start_of_month
            )

            yield month

    def shuffle_and_join(self, data, columns_to_shuffle,
                         start_column="Absence Start Date", end_column="Absence End Date"
                         ):
        """
        Iterate through each month, extract monthly data, shuffle and join into a data frame.
        Parameters
        ----------
        data: pandas.DataFrame
            the data set to monthly shuffle
        columns_to_shuffle: str
            the column of 'data' to shuffle
        start_column: str
            the column to treat as month start
        end_column: str
            the column to treat as month end

        Returns
        -------
        pandas.DataFrame
        """
        monthly_data = self.monthly_shuffle(data, columns_to_shuffle, start_column, end_column)
        return pd.concat(monthly_data)

    def grouped_monthly_shuffle(self,
                                data, groupby, columns_to_shuffle,
                                date_columns=None
                                ):
        """
        Carry out the monthly extract-shuffle with an aggregated DataFrame.
        Does the shuffle for each aggregate independently.
        Parameters
        ----------
        data: pandas.DataFrame
            the data set to monthly shuffle
        groupby: str or list
            the column(s) to aggregate on to shuffle
        columns_to_shuffle: str
            the column of 'data' to shuffle
        date_columns: tuple(str)
            the columns to treat as month start and month end
        Returns
        -------
        pandas.DataFrame
        """
        start_column, end_column = date_columns or ["Absence Start Date", "Absence End Date"]
        grps = data.groupby(groupby)
        _iter = self.progres_bar_iteration(
            grps, columns_to_shuffle,
            start_column, end_column
        )
        return pd.concat(_iter)

    def progres_bar_iteration(self,
                              groups, columns_to_shuffle,
                              start_column="Absence Start Date", end_column="Absence End Date"
                              ):
        """
        Iterate through a grouped DataFrame - doing a monthly shuffle on set columns
        with a progress bar to track where the process is.
        Parameters
        ----------
        groups: pandas.GroupedDataFrame
            a grouped pandas DataFrame
        columns_to_shuffle: str
            the column of 'data' to shuffle
        start_column: str
            the column to treat as month start
        end_column: str
            the column to treat as month end
        """
        progress = ProgressBar(20, len(groups.groups))

        logging.info("Shuffle in progress:")
        for _, frame in groups:
            progress.step()
            yield self.shuffle_and_join(frame, columns_to_shuffle, start_column, end_column)


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 12 11:24:24 2022

@author: Christopher
"""
import numpy as np
import matplotlib.pyplot as plt

def defineCircle(p1, p2, p3):
    """
    Returns the center and radius of the circle passing the given 3 points.
    In case the 3 points form a line, returns (None, infinity).
    """
    temp = p2[0] * p2[0] + p2[1] * p2[1]
    bc = (p1[0] * p1[0] + p1[1] * p1[1] - temp) / 2
    cd = (temp - p3[0] * p3[0] - p3[1] * p3[1]) / 2
    det = (p1[0] - p2[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p2[1])

    if abs(det) < 1.0e-6:
        return (None, np.inf)

    # Center of circle
    cx = (bc*(p2[1] - p3[1]) - cd*(p1[1] - p2[1])) / det
    cy = ((p1[0] - p2[0]) * cd - (p2[0] - p3[0]) * bc) / det

    radius = np.sqrt((cx - p1[0])**2 + (cy - p1[1])**2)
    return ((cx, cy), radius)

def inCircle(cx, cy, radius, x, y):
    if pow((x - cx), 2) + pow((y - cy), 2) <= pow(radius, 2):
        return True
    return False

p = (0,0)
center, radius = defineCircle((0,1), (1,0), (0,-1))
print(inCircle(center[0], center[1], radius, p[0], p[1]))
if center is not None:
    plt.figure(figsize=(4, 4))
    plt.xlim([-1.2, 1.2])
    plt.ylim([-1.2, 1.2])
    plt.plot(p[0], p[1],'ro')
    circle = plt.Circle(center, radius, fill=False)
    plt.gca().add_patch(circle)


from datetime import datetime
from zoneinfo import ZoneInfo

from flask import Blueprint, render_template

from ichrisbirch.config import get_settings

settings = get_settings()

blueprint = Blueprint(
    'health',
    __name__,
    template_folder='templates/health',
    static_folder='static',
)

@blueprint.route('/')
def health():
    return render_template(
        'health/index.html',
        settings=settings,
        server_time=datetime.now().isoformat(),
        local_time=datetime.now(tz=ZoneInfo('America/Chicago')).isoformat(),
    )


import os
import sys
import csv
import numpy as np
import pandas as pd
import scipy
import sklearn
from sklearn.model_selection import KFold
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image
import gc
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch import Tensor
from torch.utils.data import DataLoader, Dataset
import torchvision
import torchvision.transforms as T
import torchvision.models as models
from torchvision import transforms

class SpygrModule(nn.Module):
    def __init__(self, x, device):
        super().__init__()
        self.x = x
        self.x.to(device)
        self.M = 64
        self.device = device
        self.phi_conv = nn.Conv2d(x.shape[1], self.M, kernel_size=3, stride=1, padding=1, bias=True)
        self.glob_pool = nn.AvgPool2d((x.shape[2], x.shape[3]))
        self.glob_conv = nn.Conv2d(x.shape[1], self.M, kernel_size=1, stride=1, padding=0, bias=False)
        self.graph_weight = nn.Conv2d(self.x.shape[1], self.x.shape[1], kernel_size=1, stride=1, padding=0, bias=False)
        self.ReLu = nn.ReLU()

    def forward(self):
        x_phi_conv = self.phi_conv(self.x)
        x_phi = x_phi_conv.view([x_phi_conv.shape[0], -1, self.M])
        x_phi = self.ReLu(x_phi)
        x_phi_T = x_phi_conv.view([x_phi_conv.shape[0], self.M, -1])
        x_phi_T = self.ReLu(x_phi_T)

        x_glob_pool = self.glob_pool(self.x)
        x_glob_conv = self.glob_conv(x_glob_pool)
        x_glob_diag = torch.zeros(x_glob_conv.shape[0], x_glob_conv.shape[1], x_glob_conv.shape[1]).to(self.device)

        for i in range(x_glob_conv.shape[0]):
            x_glob_diag[i, :, :] = torch.diag(x_glob_conv[i, :, :, :].reshape(1, x_glob_conv.shape[1]))

        A_tilde = torch.matmul(torch.matmul(x_phi, x_glob_diag), x_phi_T)
        # print(A_tilde)
        D_tilde = torch.zeros_like(A_tilde).to(self.device)
        temp_sum = torch.sum(A_tilde, 2)
        for i in range(D_tilde.shape[0]):
            D_tilde[i, :, :] = torch.diag(temp_sum[i, :])
        # D_tilde = torch.diag(torch.sum(A_tilde, 2))

        # D_inv = torch.inverse(torch.sqrt(D_tilde))
        # D_inv = torch.linalg.inv(torch.sqrt(D_tilde))
        D_inv = D_tilde - A_tilde ## To Fix

        I = torch.eye(D_inv.shape[1]).to(self.device)
        I = I.repeat(D_inv.shape[0], 1, 1)

        L_tilde = I - torch.matmul(torch.matmul(D_inv, A_tilde), D_inv)

        output = torch.matmul(L_tilde, self.x.reshape(self.x.shape[0], -1, self.x.shape[1]))
        # print(output)
        output = output.reshape(self.x.shape[0], self.x.shape[1], self.x.shape[2], self.x.shape[3])
        output = self.graph_weight(output)
        output = self.ReLu(output)

        return output

class upconv(nn.Module):
    def __init__(self, in_channels, out_channels, ratio=2):
        super().__init__()
        self.elu = nn.ELU()
        self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, bias=False, kernel_size=3, stride=1, padding=1)
        self.ratio = ratio

    def forward(self, x):
        up_x = F.interpolate(x, scale_factor=self.ratio, mode='nearest')
        out = self.conv(up_x)
        out = self.elu(out)
        return out

class SpyGRSS(nn.Module):
    def __init__(self, device):
        super().__init__()

        self.device = device

        self.pretrained_resnet = models.resnet50(pretrained=True).to(self.device)

        for param in self.pretrained_resnet.parameters():
            param.requires_grad_(False)

        ## Layer ##
        self.reduce_dim = nn.Conv2d(2048, 512, kernel_size=3, stride=1, padding=1, bias=False)

        self.down_samp = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)

    def forward(self, x):
        x = self.pretrained_resnet.conv1(x)
        x = self.pretrained_resnet.bn1(x)
        x = self.pretrained_resnet.relu(x)
        x = self.pretrained_resnet.maxpool(x)
        x = self.pretrained_resnet.layer1(x)
        x = self.pretrained_resnet.layer2(x)
        x = self.pretrained_resnet.layer3(x)
        x = self.pretrained_resnet.layer4(x)
        x = self.reduce_dim(x)

        GR_1 = SpygrModule(x, self.device).to(self.device)

        x = self.down_samp(x)

        GR_2 = SpygrModule(x, self.device).to(self.device)

        x = self.down_samp(x)

        GR_3 = SpygrModule(x, self.device).to(self.device)

        x_gr_1 = GR_1.forward()
        x_gr_2 = GR_2.forward()
        x_gr_3 = GR_3.forward()
        output = x_gr_2 + F.interpolate(x_gr_3, scale_factor=2, mode="nearest")
        output = x_gr_1 + F.interpolate(output, scale_factor=2, mode="nearest")

        final_upsampling = upconv(512, 3, ratio=16).to(self.device)
        output = final_upsampling.forward(output)

        return output

def main():
    gc.collect()
    torch.cuda.empty_cache()
    device = torch.device("cuda")

    print(torch.cuda.is_available())
    print(torch.device('cuda:0'))

    temp = Image.open("D:/dataset/gtFine_trainvaltest/gtFine/train/aachen/aachen_000000_000019_gtFine_color.png").convert("RGB")
    temp_img = np.array(temp)
    temp_tf = T.Compose([
        T.ToTensor(),
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])
    temp_img = temp_tf(temp)
    temp_img = torch.stack([temp_img, temp_img]).to(device)

    # temp_model = SpyGRSS(device)
    temp_model = SpyGRSS(device)
    temp_model.to(device)
    temp_model.cuda()
    output = temp_model.forward(temp_img)
    # print(output.shape)
    temp_tff = T.ToPILImage()
    output = temp_tff(output[0, :, :, :])
    output.show()

    # temp_test = torch.randn(3, 3, 3)
    # temp_test.to(device)
    # print("success")

if __name__ == "__main__":
    main()