import copy
import random
import draw_config
from collections import namedtuple
from PIL import Image, ImageDraw, ImageFont

BoardLayout = namedtuple('BoardLayout', ['orient', 'layout'])

class Board:
    def __init__(self, enemy_white):
        self.enemy_white = enemy_white

        self._bot = BoardLayout(1, dict.fromkeys(
            [12, 14, 16, 18, 21, 23, 25, 27, 32, 34, 36, 38], False))
        self._enemy = BoardLayout(-1, dict.fromkeys(
            [61, 63, 65, 67, 72, 74, 76, 78, 81, 83, 85, 87], False))

    @staticmethod
    def make_move(pos, target, me, enemy):
        """Returns True if it was king's move"""
        y = me.orient if (target - pos) * me.orient > 0 else -me.orient
        x = 1 if target % 10 - pos % 10 > 0 else -1

        current_pos = pos + y * 10 + x
        while current_pos != target:
            if current_pos in enemy.layout:
                enemy.layout.pop(current_pos)

            current_pos = current_pos + y * 10 + x

        row = target // 10
        if (row, me.orient) in ((8, 1), (1, -1)):
            me.layout[target] = True
            me.layout.pop(pos)
        else:
            me.layout[target] = me.layout.pop(pos)

        if me.layout[target]:
            return True
        else:
            return False

    def move_options(self, target, bot):
        moves = {}
        compelled_board = False

        me, enemy = self.get_sides(bot)

        if target == -1:
            source = me.layout
        else:
            source = {target: me.layout[target]}

        for pos, is_king in source.items():
            if is_king:
                moves, compelled_board = self._add_options(
                    *self._king_options(pos, me, enemy), moves,
                    compelled_board)
            else:
                moves, compelled_board = self._add_options(
                    *self._men_options(pos, me, enemy), moves,
                    compelled_board)

        return moves, compelled_board

    def get_sides(self, bot):
        me, enemy = self._enemy, self._bot
        if bot:
            me, enemy = enemy, me

        return me, enemy

    def bot_score(self, bot):
        score = 0

        score += 4 * self._side_score(self._bot)
        score -= 3 * self._side_score(self._enemy)

        if bot:
            return score
        else:
            return -score

    def _side_score(self, me):
        score = 0

        for pos, is_king in me.layout.items():
            if is_king:
                score += 10
            else:
                score += 5
                if (pos // 10, me.orient) in ((1, 1), (8, -1)):
                    score += 2

            for cell in (pos - 11 * me.orient, pos - 9 * me.orient):
                if (cell in me.layout) or (not self._is_valid(cell)):
                    score += 1

        return score

    @staticmethod
    def _add_options(pos_options, pos, compelled_pos, moves,
                     compelled_board):
        if len(pos_options) > 0:
            if compelled_pos == compelled_board:
                moves[pos] = pos_options

            elif not compelled_board:
                compelled_board = True
                moves = {pos: pos_options}

        return moves, compelled_board

    def _king_options(self, pos, me, enemy):
        pos_options = set()
        compelled_pos = False

        for direction in ((-1, -1), (-1, 1), (1, -1), (1, 1)):
            met_enemy = False
            opt = pos + 10 * direction[0] + direction[1]

            while self._is_valid(opt):
                if opt in enemy.layout:
                    if opt + 10 * direction[0] + direction[1] in enemy.layout:
                        break
                    met_enemy = True
                elif opt in me.layout:
                    break
                elif met_enemy == compelled_pos:
                    pos_options.add(opt)
                elif met_enemy:
                    compelled_pos = True
                    pos_options = {opt}

                opt += 10 * direction[0] + direction[1]

        return pos_options, pos, compelled_pos

    def _men_options(self, pos, me, enemy):
        pos_options = set()
        compelled_pos = False

        for opt in ((pos - 11 * me.orient, pos - 22 * me.orient),
                    (pos - 9 * me.orient, pos - 18 * me.orient),
                    (pos + 11 * me.orient, pos + 22 * me.orient),
                    (pos + 9 * me.orient, pos + 18 * me.orient)):

            if (opt[0] in enemy.layout) and self._free_cell(opt[1], me, enemy):
                if compelled_pos:
                    pos_options.add(opt[1])
                else:
                    compelled_pos = True
                    pos_options = {opt[1]}

            elif not compelled_pos and self._free_cell(opt[0], me, enemy) and \
                    ((opt[0] - pos) * me.orient > 0):
                pos_options.add(opt[0])

        return pos_options, pos, compelled_pos

    @staticmethod
    def _is_valid(pos):
        return 1 <= pos // 10 <= 8 and 1 <= pos % 10 <= 8

    @staticmethod
    def _free_cell(pos, me, enemy):
        return Board._is_valid(pos) and \
               (pos not in enemy.layout) and (pos not in me.layout)

def to_str(checker, enemy_white):
    return to_str_format(checker // 10 - 1, checker % 10 - 1, enemy_white)

def to_str_format(row, col, enemy_white):
    if enemy_white:
        row = 7 - row
        col = 7 - col

    return chr(ord('A') + 7 - col) + str(row + 1)

class Painter:
    def __init__(self):
        self._white_blank = self._draw_blank_board(enemy_white=True)
        self._black_blank = self._draw_blank_board(enemy_white=False)

    def draw(self, board):
        if board.enemy_white:
            image = self._white_blank.copy()
        else:
            image = self._black_blank.copy()

        canvas = ImageDraw.Draw(image)

        enemy_palette = ('white', draw_config.black_draught_color,
                         draw_config.edge_color)
        bot_palette = ('black', draw_config.white_draught_color,
                       draw_config.edge_color)

        if board.enemy_white:
            enemy_palette, bot_palette = bot_palette, enemy_palette

        bot, enemy = board.get_sides(bot=True)

        self._draw_side(canvas, enemy.layout, enemy_palette, board.enemy_white)
        self._draw_side(canvas, bot.layout, bot_palette, board.enemy_white)

        image.save('tmp.png')

    def _draw_blank_board(self, enemy_white):
        image = Image.new('RGB', (550, 550), draw_config.white_cell_color)
        canvas = ImageDraw.Draw(image)

        canvas.rectangle(self._square_place(3, 515),
                         fill=draw_config.black_cell_color)
        canvas.rectangle(self._square_place(0, 512),
                         fill=draw_config.white_cell_color)

        font = ImageFont.truetype(*draw_config.font)

        for row in range(8):
            for col in range(8):
                if (row + col) % 2 == 1:
                    canvas.rectangle(self._cell_place(row, col),
                                     fill=draw_config.black_cell_color,
                                     outline=draw_config.black_cell_color)

                    text = to_str_format(row, col, enemy_white)
                    canvas.text(self._text_place(row, col, text, font, 'b'),
                                text, font=font, fill='white')

        return image

    def _draw_side(self, canvas, layout, palette, enemy_white):
        for checker, is_king in layout.items():
            if is_king:
                self._draw_checker(canvas, checker, *palette[:-1],
                                   draw_config.red_color, enemy_white)
            else:
                self._draw_checker(canvas, checker, *palette, enemy_white)

    def _draw_checker(self, canvas, checker, font_color, main_color,
                      other_color, enemy_white):

        row = checker // 10 - 1
        col = checker % 10 - 1

        canvas.ellipse(self._ellipse_place(row, col, 10, 5), fill=other_color)

        canvas.ellipse(self._ellipse_place(row, col, 5, 10),
                       fill=main_color, outline=other_color)

        font = ImageFont.truetype(*draw_config.font)
        text = to_str_format(row, col, enemy_white)
        canvas.text(self._text_place(row, col, text, font, 'c'), text,
                    font=font, fill=font_color)

    @staticmethod
    def _square_place(up_left, down_right):
        return (draw_config.margin - up_left, draw_config.margin - up_left,
                draw_config.margin + down_right,
                draw_config.margin + down_right)

    @staticmethod
    def _cell_place(row, col):
        return (col * 64 + draw_config.margin, row * 64 + draw_config.margin,
                (col + 1) * 64 + draw_config.margin,
                (row + 1) * 64 + draw_config.margin)

    @staticmethod
    def _ellipse_place(row, col, up_shift, down_shift):
        return (col * 64 + draw_config.margin + 5,
                row * 64 + draw_config.margin + up_shift,
                (col + 1) * 64 + draw_config.margin - 5,
                (row + 1) * 64 + draw_config.margin - down_shift)

    @staticmethod
    def _text_place(row, col, text, font, context):

        width, height = font.getsize(text)

        return (draw_config.margin + col * 64 + (64 - width) // 2,
                (draw_config.margin + row * 64 + 3 + (49 - height) // 2
                 if context == 'c' else
                 draw_config.margin + row * 64 + (64 - height) // 2 - 1))

class ArtificialIntelligence:
    def bot_move(self, board):
        moves, compelled_board = board.move_options(-1, bot=True)
        king_move = False
        if len(moves) == 0:
            return [], king_move

        moves_done = []

        first_move = True
        murder = -1
        while first_move or murder != -1:
            first_move = False

            score, best_moves = self._variant_score(board, depth=5, bot=True,
                                                    murder=murder)

            best_moves = self._remove_far(best_moves)

            pos, target = random.choice(best_moves)
            king_move = board.make_move(pos, target,
                                        *board.get_sides(bot=True))
            moves_done.append((to_str(pos, board.enemy_white),
                               to_str(target, board.enemy_white)))

            if compelled_board and board.move_options(target, bot=True)[1]:
                murder = target
            else:
                murder = -1

        return moves_done, king_move

    @staticmethod
    def _remove_far(best_moves):
        new_list = []
        best_dist = 0

        for move in best_moves:
            row = move[1] // 10

            if row > best_dist:
                best_dist = row
                new_list = [move]
            elif row == best_dist:
                new_list.append(move)

        return new_list

    def _variant_score(self, board, depth, bot, murder):
        if depth == 0:
            return self._change_sign(board.bot_score(bot), murder), []

        best_score = -1000
        best_moves = []

        moves, compelled_board = board.move_options(murder, bot=bot)
        if len(moves) == 0:
            return self._change_sign(-1000, murder), []

        for pos, pos_options in moves.items():
            for target in pos_options:
                move_board = copy.deepcopy(board)
                move_board.make_move(pos, target, *move_board.get_sides(bot))

                if compelled_board and move_board.move_options(target,
                                                               bot=bot)[1]:
                    score, b_m = self._variant_score(move_board, depth - 1,
                                                     bot, target)
                else:
                    score, b_m = self._variant_score(move_board, depth - 1,
                                                     not bot, -1)

                if score > best_score:
                    best_score = score
                    best_moves = [(pos, target)]
                elif score == best_score:
                    best_moves.append((pos, target))

        return self._change_sign(best_score, murder), best_moves

    @staticmethod
    def _change_sign(score, murder):
        if murder == -1:
            return -score
        else:
            return score

class Game:
    painter = Painter()
    ai = ArtificialIntelligence()

    def __init__(self, enemy_white=True):
        self._board = Board(enemy_white=enemy_white)
        Game.painter.draw(self._board)

        self.murder = -1
        self.chosen_checker = -1
        self.until_draw = 15

    def black_first_move(self):
        moves_done, king_move = Game.ai.bot_move(self._board)
        self._update_draw(king_move)

        Game.painter.draw(self._board)
        return moves_done

    def button_variants(self):
        moves, c_b = self._board.move_options(-1, bot=False)
        if self.chosen_checker == -1:
            res = moves.keys()
        else:
            res = moves[self.chosen_checker]

        return [to_str(elem, self._board.enemy_white) for elem in list(res)]

    def external_session(self, ans):
        if ans not in self.button_variants():
            return (2 if self.chosen_checker == -1 else 1), []

        if self.chosen_checker == -1:
            self.chosen_checker = self._to_pos(ans, self._board.enemy_white)
            return 4, []
        else:
            return self.move_session(self.chosen_checker,
                                     self._to_pos(ans,
                                                  self._board.enemy_white))

    @staticmethod
    def _to_pos(ans, enemy_white):
        if enemy_white:
            return (9 - int(ans[1])) * 10 + (ord(ans[0]) - ord('A') + 1)
        else:
            return int(ans[1]) * 10 + (8 - ord(ans[0]) + ord('A'))

    def _update_draw(self, king_move):
        if king_move:
            self.until_draw -= 1
        else:
            self.until_draw = 15

    def move_session(self, pos, target):  #
        """Returns 0 if you lose, 3 if choose checker, 4 if choose cell,
        5 if you win, 6 if choose target again, 7 if draw"""

        if self.murder == -1:
            moves, compelled_board = self._board.move_options(-1, bot=False)
        else:
            moves, compelled_board = self._board.move_options(self.murder,
                                                              bot=False)

        king_move = self._board.make_move(pos, target,
                                          *self._board.get_sides(bot=False))
        self._update_draw(king_move)

        if compelled_board and self._board.move_options(target, bot=False)[1]:
            self.murder = target
            self.chosen_checker = target
            Game.painter.draw(self._board)
            return 6, []
        else:
            self.murder = -1
            self.chosen_checker = -1

        moves_done, king_move = Game.ai.bot_move(self._board)
        self._update_draw(king_move)
        Game.painter.draw(self._board)

        if len(moves_done) == 0:
            return 5, moves_done

        moves, compelled_board = self._board.move_options(-1, bot=False)

        if len(moves) > 0:
            if self.until_draw > 0:
                return 3, moves_done
            else:
                return 7, moves_done
        else:
            return 0, moves_done


from torch.utils.data import Dataset, DataLoader
import os
import pandas as pd
from PIL import Image
import numpy as np
from torchvision import transforms
from torch.utils.data.sampler import SubsetRandomSampler
from skimage import io

class DatasetCells(Dataset):
    """An abstract class representing a Dataset.
    All other datasets should subclass it. All subclasses should override
    ``__len__``, that provides the size of the dataset, and ``__getitem__``,
    supporting integer indexing in range from 0 to len(self) exclusive.
    """
    def __init__(self,file_data_idx, file_label_idx, file_inter_idx, file_contour_idx, transform=None, mode="train"):
        self.data_root = "../data/GenData/"
        #self.data = pd.read_csv(file_path)
        self.file_data_idx = file_data_idx
        self.file_label_idx = file_label_idx
        self.file_inter_idx = file_inter_idx
        self.file_inter_idx = file_contour_idx
        self.transform = transform
        self.mode = mode
        if self.mode is "train":
          self.data_dir = os.path.join(self.data_root, "TrainData/images/")
          self.label_dir = os.path.join(self.data_root, "TrainData/labels_inter/")
          self.inter_dir = os.path.join(self.data_root, "TrainData/watershed/")
          self.contour_dir = os.path.join(self.data_root, "TrainData/labels/")
        elif self.mode is "validation":
            pass
        elif self.mode is "test":
            pass

    def __len__(self):
        return len(self.file_data_idx)

    def __getitem__(self, index):
        # load image as ndarray type (Height * Width * Channels)
        # be carefull for converting dtype to np.uint8 [Unsigned integer (0 to 255)]
        # in this example, i don't use ToTensor() method of torchvision.transforms
        # so you can convert numpy ndarray shape to tensor in PyTorch (H, W, C) --> (C, H, W)
        #image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((1, 28, 28))
        #label = self.data.iloc[index, 0]
        #train_id = self.file_data_idx["ids"].iloc[index]
        #label_id = self.file_data_idx["ids"].iloc[index]
        if self.mode is "train":
          file_id = self.file_data_idx["ids"].iloc[index]
          self.image_path = os.path.join(self.data_dir, file_id)
          self.label_path = os.path.join(self.label_dir, file_id)# file id should be from label data, buit in this case is the same.
          self.inter_path = os.path.join(self.inter_dir, file_id)
          self.contour_path = os.path.join(self.contour_dir, file_id)
          image = io.imread(self.image_path)
          label = io.imread(self.label_path)
          inter = io.imread(self.inter_path)
          contour = io.imread(self.contour_path)
          #image = Image.open(self.image_path).convert("L")# L -> grayscale channel
          #label = Image.open(self.label_path)
          #inter = Image.open(self.inter_path)
          #contour = Image.open(self.contour_path)

          if self.transform is not None:
              image = self.transform(image)
              label = self.transform(label)
              inter = self.transform(inter)
              contour = self.transform(contour)
          return image, label, inter, contour
        if self.mode is "validation":
            pass
        if self.mode is "test":
            pass

    def __add__(self, other):
        return ConcatDataset([self, other])

def CellTrainData(data_transform=None, mode="train"):
    file_idxs = pd.read_csv("../data/GenData/train_input_ids.csv")
    label_idxs = pd.read_csv("../data/GenData/train_labels_ids.csv")
    inter_idxs = pd.read_csv("../data/GenData/train_inter_ids.csv")
    contour_idxs = pd.read_csv("../data/GenData/train_contour_ids.csv")
    dataset = DatasetCells(file_idxs, label_idxs, inter_idx, contour_idxs, transform=data_transform, mode=mode)
    return dataset

def CellDataLoader(data_transform=None, mode="train", batch_sz=2, workers=1):
    file_idxs = pd.read_csv("../data/GenData/train_input_ids.csv")
    label_idxs = pd.read_csv("../data/GenData/train_labels_ids.csv")
    inter_idxs = pd.read_csv("../data/GenData/train_inter_ids.csv")
    contour_idxs = pd.read_csv("../data/GenData/train_contour_ids.csv")
    if data_transform is None:
        data_transform = transforms.ToTensor()
    dataset = DatasetCells(file_idxs, label_idxs, inter_idxs, contour_idxs, transform=data_transform, mode="train")
    dataloader = DataLoader(dataset, batch_size=batch_sz, num_workers=workers, shuffle=True)
    return dataloader

def CellTrainValidLoader(data_transform=None, validation_split=0.1, mode="train", batch_sz=2, workers=1):
    file_idxs = pd.read_csv("../data/GenData/train_input_ids.csv")
    label_idxs = pd.read_csv("../data/GenData/train_labels_ids.csv")
    inter_idxs = pd.read_csv("../data/GenData/train_inter_ids.csv")
    contour_idxs = pd.read_csv("../data/GenData/train_contour_ids.csv")
    if data_transform is None:
        data_transform = transforms.ToTensor()
    dataset = DatasetCells(file_idxs, label_idxs,inter_idxs, contour_idxs, transform=data_transform, mode="train")

    shuffle_dataset = True
    random_seed = 1234

    dataset_size = len(dataset)
    indices = list(range(dataset_size))
    split = int(np.floor(validation_split * dataset_size))
    if shuffle_dataset:
        np.random.seed(random_seed)
        np.random.shuffle(indices)

    train_indices, val_indices = indices[split:], indices[:split]
    train_sampler = SubsetRandomSampler(train_indices)
    valid_sampler = SubsetRandomSampler(val_indices)

    train_loader = DataLoader(dataset, batch_size=batch_sz, sampler = train_sampler, num_workers=workers)
    validation_loader = DataLoader(dataset, batch_size=batch_sz, sampler = valid_sampler, num_workers=workers)

    return train_loader, validation_loader


from State2 import *
from Utils import *

# Depth First Search - pesquisa em profundidade
def dfs(state: State2, goal: int = 2, max_depth: int = 15):
    root = Node(state)
    stack = [root]
    tree = Graph(root)

    # list containing states (x,y) which were already discovered (this is an improvement)
    visited = []
    # if the stack is empty we also want to terminate the loop as no more nodes are expandable
    while len(stack) != 0:
        # popping the first node on the stack (current highest depth node)
        node = stack.pop(0)
        # adding the node to the visited list, we do this here so when we are expanding the nodes
        # we dont add to the stack nodes already discovered, including the parent
        visited.append(node.info.final)

        # here we expand said node
        expanded = expand(node.info)

        if node.depth != max_depth:
            # for each expanded node we want to add it to the tree, and insert it on the stack so on
            # the next loop we start by the highest depth nodes
            for x in expanded:
                if x.final not in visited:
                    new_node = Node(x)
                    tree.add_node(new_node)
                    node.add_edge(new_node, 1)
                    stack.insert(0, new_node)

        # The solution may not be optimal, but it's faster than the BFS method, and takes less space
        solution = contains_goal(stack, goal)

        if solution is not None:
            print("Found Solution, Depth", solution.depth)
            return solution, tree

    return None, tree


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

# In[ ]:

import networkx as nx
import pandas as pd
import pickle
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from collections import Counter
import warnings
import os
import time
#import nxviz as nv
from scipy import stats
from datetime import datetime
from sklearn import metrics

warnings.filterwarnings('ignore')  # suppress warning messages
get_ipython().run_line_magic('matplotlib', 'inline')
sns.set()

# In[ ]:

# Import artist_dictionary.data, tags_dict.data, and yahoo ratings.csv
# from working directory.

artist_dictionary = pickle.load(open('artist_dictionary.data','rb'))
tags_dictionary = pickle.load(open('tags_dict.data','rb'))
filtered_tags_dictionary = pickle.load(open('filtered_tags_dict.data','rb'))

ratings = pd.read_csv('artist-yahoo-ratings.csv')
ratings['artist'] = ratings['artist_name'].str.lower()
ratings = ratings.set_index('artist')
# fill missing ratings with 0 and convert column to integer
ratings['yahoo_rating_count'] = ratings['yahoo_rating_count'].fillna(0).astype(int)

spotify_rankings = pd.read_csv('10k_spotify_rankings.csv')

spotify_rankings['Artist'] = spotify_rankings['Artist'].str.lower()

# lower-case all artists - also running at end of cell to make manually adding artists easier

artist_dictionary = {str(key).lower():artist_dictionary[key] for key in artist_dictionary.keys()}

for key in artist_dictionary.keys():
    artist_dictionary[key]=[i.lower() for i in artist_dictionary[key]]

tags_dictionary = {str(key).lower():tags_dictionary[key] for key in tags_dictionary.keys()}

for key in tags_dictionary.keys():
    tags_dictionary[key]=[str(i).lower() for i in tags_dictionary[key]]

filtered_tags_dictionary = {str(key).lower():filtered_tags_dictionary[key] for key in filtered_tags_dictionary.keys()}

for key in filtered_tags_dictionary.keys():
    filtered_tags_dictionary[key]=[str(i).lower() for i in filtered_tags_dictionary[key]]

#remove duplicate values from each key
artist_dictionary = {key:list(set(artist_dictionary[key])) for key in artist_dictionary.keys()}
tags_dictionary = {key:list(set(tags_dictionary[key])) for key in tags_dictionary.keys()}

# In[ ]:

def get_edges(dictionary,label, pop_weight = True, weight = 0):
    """Create a list of edges from a dictionary.

    Create a list of labeled edges for a network graph by comparing the values (artists)
    of each key (band) in a dictionary.  If two values share a key, return a list of
    (key,key,{link:shared value}) tuples.  This function is called by the build_net function to
    generate the edge list for graph construction.

    Parameters
    ----------
    dictionary : dict

    label : str
            Label to be used as key in edge metadata, used to describe node relationship.

    Returns
    -------
    edgelist : list of str and dict

    """
    edgelist_raw = []
    for  key, value in dictionary.items():
        for items in value:
            for key1, value1 in dictionary.items():
                for items1 in value1:
                    if len(items) > 1 and len(items1) > 1: # Prevent single letter artists
                                                         # ('G' or 'K') from linking with
                                                         # every artist with a g or k in their name.

                        if items in items1:
                            if pop_weight == True:
                                # adding to the edge attributes the average of the Yahoo! rating count between two artists

                                #solve for df having multiple entires for artist ratings
                                if key in ratings.index:
                                    rating1 = ratings.loc[key, 'yahoo_rating_count'].astype(int)

                                    # TEMP - there may be multiple artists in the ratings file.  if this is the case,
                                    # select the first value in the series.  Remove when ratings file is consolidated.
                                    if type(rating1) == pd.core.series.Series:
                                        rating1 = list(ratings.loc[key, 'yahoo_rating_count'])[0]
                                else:
                                    rating1 = 1

                                if key1 in ratings.index:
                                    rating2 = ratings.loc[key1, 'yahoo_rating_count'].astype(int)

                                    if type(rating2) == pd.core.series.Series:
                                        rating2 = list(ratings.loc[key1, 'yahoo_rating_count'])[0]
                                else:
                                    rating2 = 1

                                avg_rating = (rating1 + rating2) / 2 # average rating between two connected artists
                                                                     # ex. The Beatles (1000) <--> The Who (500)
                                                                     # = 750 edge weight.

                                norm_rating = avg_rating / 783871    # naive normalize average rating onto a 0 - 1 scale.
                                                                     # 783871 is the highest rating in the Yahoo! data

                               # # check to that the edge does not already exist in edge_list
                               # if (key,key1,{label:items, 'weight':norm_rating}) not in edgelist_raw:

                                edgelist_raw.append((key,key1,{'kind':label, 'link':items, 'weight': norm_rating}))

                            # return edgelist without edge weight passed to function
                            else:
                                edgelist_raw.append((key,key1,{'kind':label, 'link':items, 'weight': weight}))

    # remove duplicate edges from edgelist_raw
    edgelist = []
    edgelist = [i for i in edgelist_raw if i not in edgelist]

    return edgelist

def build_net(seed, goal=None, size=600, graph=True):
    """ Build an artist-based network graph.

    Create an undirected, artist-based network graph with labeled edges. Basic
    graph structure is equivelant to (band1) <-- shared artist --> (band2). By
    default, function will build networks of size 600 (node count / bands.)
    Optionally, the function can also produce a network that starts with a
    band (seed) and builds a network until it contains the a second band (goal.)
    Warning! There is no check for whether two bands reside in the same network.  It is
    possible to specify a seed and goal for which a relationship does not exist.

    Parameters
    ----------
    seed : str
           Seed band around wihch a network graph is built.  Also root of tree search.

    goal : str
            The function will stop growing the network graph once it contains the goal (band).
            Optional, default = None.

    size : int
           Size of the network graph, defined by node count.
           Optional, default = 600 nodes (bands.)

    graph : bool
            True = return graph object, False = return node/edge lists.

    Returns
    -------
    graph : networkX graph object
            if graph=True, return undirected graph

    nodes,edges : list of str
                  if graph=False, return node and edge lists

    """
    artist_list = []
    band_list = []

    artistindex = 0  # Use index markers to keep track of progress and
    bandindex = 0    # avoid iterating over the entire artist and band lists.

    band_list.append(seed)  # Level 1 seed band (root).

    if goal is not None:

        if nx.has_path(seed,goal) == True:  # Check that a path between seed and goal exists.

            while goal not in band_list:
                for band in band_list[bandindex:]:               # Only retrieve artists for 'new' bands
                    for artist in artist_dictionary[band]:       # in band_list.
                        if artist not in artist_list:
                            artist_list.append(artist)           # Add new artists to artist_list.

                bandindex = len(band_list)                       # Mark current bands in band_list as 'worked'
                                                                 # by setting index marker to the first index
                                                                 # following last element in band_list.

                for artist in artist_list[artistindex:]:         # Only retrieve bands for 'new' artists
                    for key, value in artist_dictionary.items(): # in artist_list.
                        if artist in value:
                            if key not in band_list:
                                if goal not in band_list:
                                    band_list.append(key)        # Add new bands to band_list.

                artistindex = len(artist_list)                  # Mark current artists in artist_list as 'worked'
                                                                 # by setting index marker to the first index
        else:                                                    # following the last element in artist_list.

            print('A path between ',seed,' and ',goal,' does not exist.')

    else:
        prev_band_len = 0
        prev_art_len = 0
        go = True                                   # exit var will flip and end loop if no new artists e
                                                    # are added to the band list (see note below)
        while len(band_list) < size and go == True:

            if len(band_list) > prev_band_len or len(artist_list) > prev_art_len:
                                                        # check that the len of bandlist has increased, if not
                prev_art_len = len(artist_list)         # set go = no.  added to prevent infinite loops caused
                prev_band_len = len(band_list)          # by artists who have only worked solo ex Madonna.

                for band in band_list[bandindex:]:
                    for artist in artist_dictionary[band]:
                        if artist not in artist_list:
                            artist_list.append(artist)

                bandindex = len(band_list) -1

                for artist in artist_list[artistindex:]:
                    for key, value in artist_dictionary.items():
                        if artist in value:
                            if key not in band_list:
                                if len(band_list) < size:
                                    band_list.append(key)

                artistindex = len(artist_list) -1

            else:
                go = False

    dictionary = {band : artist_dictionary[band] for band in band_list}  # local artist dictionary

    for key, value in dictionary.items():
        dictionary[key] = set(value)   # filter out duplicate group members.

    nodes = list(dictionary.keys())

    edges = get_edges(dictionary, 'Artist')

    for i in edges:              # remove single edge loops from edge list
            if i[0] == i[1]:     # ex. ('The Who','The Who',{'artist':'Roger Daltrey'})
                edges.remove(i)

    if graph == False:

        return(nodes, edges)

    else:
        net_graph = nx.MultiGraph()
        net_graph.add_nodes_from(nodes)
        net_graph.add_edges_from(edges)
        return(net_graph)

# In[ ]:

def add_tag_edges(graph, just_edges=False):
    """Add user tag-based edges to an existing artist network graph.

    Append user tag-based edges to an existing artist graph and return a
    multigraph. Creates edges in the form of (band1,band2,{link:tag})
    tuple.  This function will not add new nodes to the graph.

    Parameters
    ----------
    graph : graph object
            Network graph from which tags will be retrieved, and tag-edges will be added

    just_edges : bool
                 If just_edges = True, return new, undirected multigraph object.
                 If just_edges = False, return list of tag-based edges.

    Returns
    -------
    new_graph : networkX multigraph object

    tag_edges : list of str

    """

    new_graph = nx.MultiGraph()

    #new_graph.add_nodes_from(graph.nodes()) # add nodes from original graph

    new_graph.add_edges_from(graph.edges.data()) # add edges w/attribute dictionary from original graph

    bands = [node for node in graph.nodes()]

    dictionary = {band : tags_dictionary[band] for band in bands
                  if band in tags_dictionary.keys()}  # Check if band exists as a key in
                                                      # in tags_dictionary.

    #if len(dictionary.keys()) >= 2:                   # comparing keys requires atleast two keys

    tag_edges = get_edges(dictionary,'User-Tag', pop_weight = False, weight = 0.05) #<<<--- hard-coded tag-edge weight.

    for i in tag_edges:          # remove single edge loops from edge list
        if i[0] == i[1]:         # ex. ('The Who','The Who',{'LINK':'classic rock'})
            tag_edges.remove(i)

    if len(tag_edges) > 0:
        if just_edges == True:

            return tag_edges

        else:
            new_graph.add_edges_from(tag_edges)

            return new_graph

def new_centrality(graph):
    """ Compute centrality scores for a network graph.

    Compute a number of different centrality and misc. scores for all nodes in a network graph.

    Parameters
    ----------
    graph : networkX graph object

    Returns
    -------
    core_df : Pandas DataFrame object

    """

    core_df = pd.DataFrame()
    core_df['artist'] = graph.nodes() # Add to the artist column all nodes (artists) in a graph.
    scores_list = []

    try:
        deg_cent = pd.DataFrame.from_dict(nx.degree_centrality(graph), orient = 'index',  columns = ['deg_cent'])
        scores_list.append(deg_cent)
    except:
        pass

    try:
        load_cent = pd.DataFrame.from_dict(nx.load_centrality(graph), orient = 'index',  columns = ['load_cent'])
        scores_list.append(load_cent)
    #between_cent = nx.betweenness_centrality(graph)
    except:
        pass

    try:
        page_rank = pd.DataFrame.from_dict(nx.pagerank_numpy(graph), orient = 'index',  columns = ['page_rank'])
        scores_list.append(page_rank)
    except:
        pass

    try:
        ev_cent = pd.DataFrame.from_dict(nx.eigenvector_centrality_numpy(graph), orient = 'index',  columns = ['ev_cent'])
        scores_list.append(ev_cent)
    except:
        pass

    try:
        cl_cent = pd.DataFrame.from_dict(nx.closeness_centrality(graph), orient = 'index',  columns = ['close_cent'])
        scores_list.append(cl_cent)
    except:
        pass

    try:
        cfcc = pd.DataFrame.from_dict(nx.current_flow_closeness_centrality(graph), orient = 'index',  columns = ['cf_close_cent'])
        scores_list.append(cfcc)
    except:
        pass
    """
    try:
        ic = pd.DataFrame.from_dict(nx.information_centrality(graph), orient = 'index',  columns = ['info_cent'])
        scores_list.append(ic)
    except:
        pass

        #ebc = pd.DataFrame.from_dict(nx.edge_betweenness_centrality(graph), orient = 'index',  columns = ['edge_bet_cent'])

    try:
        cfbc = pd.DataFrame.from_dict(nx.current_flow_betweenness_centrality(graph), orient = 'index',  columns = ['edge_cflow_cent'])
        scores_list.append(cfbc)
    except:
        pass
    #ecfbc = pd.DataFrame.from_dict(nx.edge_current_flow_betweenness_centrality(graph), orient = 'index',  columns = ['cf_between_cent'])

    try:
        acfbc = pd.DataFrame.from_dict(nx.approximate_current_flow_betweenness_centrality(graph), orient = 'index',  columns = ['appx.cfbt_cent'])
        scores_list.append(acfbc)
    except:
        pass
    #elc = pd.DataFrame.from_dict(nx.edge_load_centrality(graph), orient = 'index',  columns = ['edge_load_cent'])
    """
    try:
        hc = pd.DataFrame.from_dict(nx.harmonic_centrality(graph), orient = 'index',  columns = ['harm_cent'])
        scores_list.append(hc)
    except:
        pass
    #d = pd.DataFrame.from_dict(nx.dispersion(graph), orient = 'index',  columns = ['dispersion'])
    """
    try:
        soc = pd.DataFrame.from_dict(nx.second_order_centrality(graph), orient = 'index',  columns = ['sec_ord_cent'])
        scores_list.append(soc)
    except:
        pass
    """
    df = pd.concat(scores_list, axis = 1)

    core_df = core_df.merge(df,
                            left_on = 'artist',
                            right_index = True)

    core_df['mean_cent'] = core_df.apply(lambda row: np.mean(row[1:]),   #  Calculate the mean of the row
                                    axis = 1)
    return core_df

# In[ ]:

def layer_graphs(artists, add_tags = True,  size = 600):
    """Layer n artist networks together.

    Layer together multiple artist-based graphs, add tag-based edges,
    and return a single multigraph.

    Parameters
    ----------
    args : str
           Band(s) around which network graphs will be built and a layered
           together.
    size : int
           Size in # of nodes for the individual artist networks.

    Returns
    -------
    graph : networkX multigraph object

    """
    layered_graph = nx.MultiGraph()

    for band in artists:
        if band in artist_dictionary.keys():

            artist_graph = build_net(band, size = size)  # Build the artist-based network

            layered_graph.add_edges_from(artist_graph.edges.data())  # Add edges to layered_graph, nodes
                                                                # will be added if they do not already exist.
        else:
            print(band, ' not in artist_dictionary')

    if add_tags == True:
        layered_graph = add_tag_edges(layered_graph)  # Add tag-based edges to layered_graph and
                                                  # connect the artist graph layers.

    return layered_graph

# In[ ]:

"""  This is a catch-all section for manually adding missing artists and bands
to the artist dictionary.  They will eventually be moved intoartist_dictionary.data.
One day..."""

artist_dictionary['bob seger'] = ['bob seger']
artist_dictionary['tom petty'] = ['tom petty']
artist_dictionary['phil collins'] = ['phil collins']
artist_dictionary['madonna'] = ['madonna']
artist_dictionary['the english beat'] = artist_dictionary['the beat']
artist_dictionary['gloria estefan'] = ['gloria estefan']
artist_dictionary['don henley'] = ['don henley']
artist_dictionary['daryl hall & john oates'] = artist_dictionary['hall & oates']
artist_dictionary['boy george'] = ['boy george']
artist_dictionary['a-ha'] = ['magne furuholmen', 'morten harket','paul waaktaar-savoy']
artist_dictionary['huey lewis & the news'] = artist_dictionary['huey lewis and the news']
artist_dictionary['ilse delange'] =  ['ilse delange']
artist_dictionary['chris isaak'] = ['chris isaak']
artist_dictionary['vanessa carlton'] = ['vanessa carlton']
artist_dictionary['alicia keys'] = ['alicia keys']
artist_dictionary['adele'] = ['adele']
artist_dictionary['amy winehouse'] = ['amy winehouse']
artist_dictionary['rihanna'] = ['rihanna']
artist_dictionary['marco borsato']= ['marco borsato']
artist_dictionary['mary j. blige'] = ['mary j. blige']
artist_dictionary['amy macdonald'] = ['amy macdonald']
artist_dictionary['tom jones'] = ['tom jones']
artist_dictionary['george michael'] = ['george michael']
artist_dictionary['james morrison'] = ['james morrison']
artist_dictionary['iggy pop'] = ['iggy pop']
artist_dictionary['joan osborne'] = ['joan osborne']
artist_dictionary['anti-flag'] = ['andy wright', 'justin sane', 'pat thetic','chris head']
artist_dictionary['kayzo'] = ['kayzo']
artist_dictionary['fame on fire'] = ['bryan kuznitz', 'blake saul', 'paul spirou', 'alex roman']
artist_dictionary['rev theory'] = ['rich luzzi', 'julien jorgensen','rikki lixx','matt mccloskey','dave agoglia']
artist_dictionary['the anix'] = ['brandon smith', 'logan smith','chris dinger']
artist_dictionary['ded'] = ['joe cotela', 'kyle koelsch', 'david ludlow', 'matt reinhard']
artist_dictionary['blind channel'] = ['joel hokka', 'niko moilanen', 'joonas porko', 'olli matela', 'tommi lalli']
artist_dictionary['mammoth mammoth'] = ['mikey tucker', 'ben couzens', 'frank trobbiani','pete bell','gareth sweet','simon jaunay','marco gennaro','kris sinister']
artist_dictionary['dayseeker'] = ['rory rodriguez', 'mike karle', 'alex polk', 'gino sgambelluri','andrew sharp']
artist_dictionary['dream state'] = ['charlotte gilpin', 'aled evans', 'rhys wilcox', 'danny rayer', 'sam harrison-little', 'jamie lee']
artist_dictionary['wage war'] = ['briton bond', 'cody quistad',  'seth blake', 'jordan pierce', 'david rau', 'chris gaylord', 'stephen kluesener']
artist_dictionary['soulja boy'] = ['']
artist_dictionary['fergie'] = ['fergie']
artist_dictionary['pitbull'] = ['pitbull']
artist_dictionary['ne-yo'] = ['ne-yo']
artist_dictionary['jessie j'] = ['jessie j']
artist_dictionary['nicki minaj'] = ['nicki minaj']
artist_dictionary['jennifer lopez'] = ['jennifer lopez']
artist_dictionary['chris brown'] = ['chris brown']
artist_dictionary['justin bieber'] = ['justin bieber']
artist_dictionary['taio cruz'] = ['taio cruz']
artist_dictionary['jason derulo'] = ['jason derulo']
artist_dictionary['flo rida'] = ['flo rida']
artist_dictionary['iyaz'] = ['iyaz']
artist_dictionary['black eyed peas'] = artist_dictionary['the black eyed peas']
artist_dictionary['alexandra stan'] = ['alexandra stan']
artist_dictionary['david guetta'] = ['david guetta']
artist_dictionary['fidlar'] = ['zac carper', 'elvis kuehn', 'max kuehn','brandon schwartzel']

artist_dictionary["swingin' utters"] = ['kevin wickersham','greg mcentee', 'darius koski', 'max huber', 'spike slawson','jack dalrymple', 'miles peck']
artist_dictionary['the 4-skins'] = ['steve harmer','gary hodges','tom mccourt']
artist_dictionary['4 skins'] = artist_dictionary['the 4-skins']
artist_dictionary['jfa']=['Mike Tracy','Brian Brannon','Don Pendleton','Corey Stretz','Todd Barnes',                          'Alan Bishop','Scott Chazan','Michael Cornelius','Bob Cox','Brian Damage',                          'Joel DuBois','Trace Element','Matt Etheridge','Jim Moore','Don Pendelton',                          'Al Penzone','Jaime Reidling','Mike Sversvold','Bruce Taylor','Mike Tracy']
artist_dictionary['nihilistics'] = ['Dave Phinster','Mike King','Ron Rancid','Ajax Lepinski']
artist_dictionary['the neighborhoods'] = ['Dave Minehan','John Hartcorn', 'Michael Quaglia']
artist_dictionary['nobodys'] = ['Geoff Palmer', 'J. J. Nobody', 'Justin Disease', 'Randy The Kid']
artist_dictionary['china white'] = ['Marc Martin','James Rodriguez','Joey Ruffino','Frank Ruffino',                                    'Scott Sisunik','Richard Katchadoorian','Vince Mesa','Corey Stretz',                                    'Steven Barrios','Jeff Porter','James Lugo','Sandy Hancock']
artist_dictionary['the fairlanes'] = ['Andy Baldwin', 'Jason Zumbrunnen', 'Robbie Kalinowski', 'Scott Weigel']
artist_dictionary['pink lincolns'] = ['Chris Barrows','Dorsey Martin','Kevin Coss','Jeff Fox']
artist_dictionary['zeke'] = ['Blind Marky Felchtone','Kyle Whitefoot','Kurt Colfelt','Dayne Porras',                             'Jeff Hiatt','Chris Johnsen','Buzzy','Kurt Colfelt','Jeff Matz',                             'Mark Pierce','Abe Zanuel Riggs III','Dizzy Lee Roth','Donny Paycheck']
artist_dictionary['x-ray spex'] = ['B.P. Hurding','Poly Styrene','Steve Thompson','Lora Logic']
artist_dictionary['fidlar'] = ['Zac Carper','Elvis Kuehn','Max Kuehn','Brandon Schwartzel']
artist_dictionary['johnny thunders & the heartbreakers'] = artist_dictionary['the heartbreakers']
artist_dictionary['mike v & the rats'] = ['Jason Hampton', 'Mike Vallely', 'P.T. Pugh', 'Reid Black']
artist_dictionary['g-eazy'] = ['g-eazy']
artist_dictionary['post malone'] = ['post malone']
artist_dictionary['lil skies'] = ['lil skies']
artist_dictionary['lil mosey'] = ['lil mosey']
artist_dictionary['youngboy never broke again'] = ['youngboy never broke again']
artist_dictionary['a boogie wit da hoodie'] =['a boogie wit da hoodie']
artist_dictionary['lil baby'] = ['ll baby']
artist_dictionary['pnb rock'] = ['pnb rock']
artist_dictionary['tee grizzley'] = ['tee grizzley']
artist_dictionary['meek mill'] = ['meek mill']
artist_dictionary['lil zay osama'] = ['lil zay osama']
artist_dictionary['montana of 300'] = ['montana of 300']
artist_dictionary['playboi'] =['playboi']
artist_dictionary['strap da fool'] = ['strap da fool']
artist_dictionary['lil durk'] = ['lil durk']
artist_dictionary['juice wrld'] = ['juice wrld']
artist_dictionary['quando rondo'] = ['quando rondo']
artist_dictionary['yk osiris'] = ['yk osiris']
artist_dictionary['pnb meen'] = ['pnb meen']
artist_dictionary['lil uzi vert'] = ['lil uzi vert']
artist_dictionary['knuckle puck'] =['Joe Taylor','Kevin Maida','John Siorek','Nick Casasanto','Ryan Rumchaks']
artist_dictionary['the story so far'] = ['Parker Cannon','Kelen Capener','Kevin Geyer','Will Levy','Ryan Torf']
artist_dictionary['derek and the dominos'] = ['Eric Clapton','Bobby Whitlock','Carl Radle','Jim Gordon','Dave Mason','Duane Allman']
artist_dictionary['derek & the dominos'] = ['Eric Clapton','Bobby Whitlock','Carl Radle','Jim Gordon','Dave Mason','Duane Allman']

#adding comment to diagnose git issues

artist_dictionary = {str(key).lower():artist_dictionary[key] for key in artist_dictionary.keys()}
for key in artist_dictionary.keys():
    artist_dictionary[key]=[i.lower() for i in artist_dictionary[key]]


# Oddny H Brun, UCF, June 2018. This program is part of an SPH DWT simulation model of max 3D
#
# create class:Particle with objects(instances) particles and assign each particle parameter values:
# ID, position, velocity, mass
# Initial particle locations random uniformly on the Tank's length, width and 1/3 of its height.
#

import numpy as np
import ohb_sph_dwt_3d as dwt_3d

np.random.seed( 3 )
part_loc_x =  np.random.rand(dwt_3d.num_of_part)*dwt_3d.Tank.length
print(part_loc_x)
print('part_loc_x[3]', part_loc_x[3])
np.random.seed( 5 )
part_loc_z =  np.random.rand(dwt_3d.num_of_part)*dwt_3d.Tank.height/.333
print('part_loc_z[3]', part_loc_z[3])
np.random.seed( 9 )
if dwt_3d.dimension == 2:
    part_loc_y = np.zeros(dwt_3d.num_of_part)
else:
    part_loc_y =  np.random.rand(dwt_3d.num_of_part)*dwt_3d.Tank.width

class Particle:

    def __init__(self, pID, particlePos,  velocity, addVelocity, addPosition):
        self.pID = pID
        self.particlePos = particlePos
        self.velocity = velocity
        self.addVelocity = addVelocity
        self.addPosition = addPosition

        self.mass = 1.0

    def updateParticles(self, addVelocity, addPosition):
        for idx in range(dwt_3d.num_of_part):
            for idy in range(3):
                particles[idx].velocity[idy] += particles[idx].addVelocity[idy]
                particles[idx].particlePos[idy] += particles[idx].addPosition[idy]
                #print(particlePos)
        return(particles)

particles = [Particle(idx, ([0.0, 0.0, 0.0]), ([0.0, 0.0, 0.0]), ([0.0, 0.0, 0.0]), ([0.0, 0.0, 0.0])) for idx in range(dwt_3d.num_of_part)]

# initial position for each particle

for idx in range(dwt_3d.num_of_part):
    particles[idx].particlePos = ([part_loc_x[idx], part_loc_y[idx], part_loc_z[idx]])
print('particles[3].particlePos, particles[3].velocity', particles[3].particlePos, particles[3].velocity)

# update velocity, position,

#print('particles[3].velocity', particles[3].velocity)

for idx in  range(dwt_3d.num_of_part):  #if dwt_3d.timesteps == 2:
    Particle.updateParticles(particles[idx], ([.1, 0.0, .3]), ([3., 0.0, 5.]))

print('particles[3].velocity', particles[3].updateParticles(3, ([.1, 0.0, .3]), ([5., 0.0, 5.])))


import sys
import os

in_file = open(sys.argv[1], 'r')

# Line Count, checks if on first line
lineCount = 0

# Reads first line and stores information
currFile = in_file.readline()
currINode = currFile.split('/')[0]
pathToLink = currFile.split(' ')[1] + ' '
firstName = currFile.split('/')[-1]

# print(pathToLink)
# print(currFile)
# print('split with "/": ' + currINode)
# print('split with " ": ' + pathToLink, end="")
# print(currName)

while True:
	# Reads subsequent lines in file
	nextFile = in_file.readline()
	# print(nextFile)
	if nextFile == "":
		break

	if lineCount == 0:
		print("Linking " + pathToLink + "as : " + firstName)
		cmd = 'ln ' + pathToLink + firstName
		os.system(cmd)
		lineCount += 1

	nextINode = nextFile.split('/')[0]
	nextName = nextFile.split('/')[-1]
	pathToLink = nextFile.split(' ')[1] + ' '
	if currINode == nextINode:
		print('Linking ' + pathToLink + 'as : ' + nextName)
		cmd = 'ln ' + pathToLink + nextName
		os.system(cmd)
		#print('Entered if statement')
	else:
		print('Linking ' + pathToLink + 'as : ' + nextName)
		cmd = 'ln ' + pathToLink + nextName
		os.system(cmd)
		currINode == nextINode
		#print('Entered else statement')


import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from skimage import io
from skimage.color import rgb2gray
from skimage.measure import compare_ssim as ssim

from unet_generator_stage1 import *
from data_loading import *

model_path = '/media/qiujing/91ec90ab-87ac-41f3-9f23-fbbbf9c36c61/U_model/stage1_mask/'

def list2json(list_name, json_path, json_name):
    json_file = open(os.path.join(json_path, json_name), 'a')
    for i in list_name:
        json_i = json.dumps(i)
        json_file.write(json_i + '\n')
    json_file.close()

def json2list(json_path, json_name):
    read_list = []
    with open(os.path.join(json_path, json_name), 'r') as f:
        new_list = f.read().split('\n')[:-1]
        for x in new_list:
            json_x = json.loads(x)
            read_list.append(json_x)
    f.close
    return read_list

train_list = json2list('/home/yiming/code/data/', 'list_train.json')

train_pair_list = []
group_id_list = []
counter = 0
for i in range(len(train_list)):
    names = train_list[i].split('_')
    img_id = names[0]
    if i == 0:
        group_id_list.append([train_list[i]])
    else:
        if img_id == train_list[i-1].split('_')[0]:
            group_id_list[counter].append(train_list[i])
        else:
            counter += 1
            group_id_list.append([train_list[i]])
#print(len(group_id_list))
for item in group_id_list:
    for i in range(len(item)):
        for j in range (i+1, len(item)):
            train_pair_list.append([item[i], item[j]])

train_flip_list = json2list('/home/yiming/code/data/', 'list_train_flip.json')

train_flip_pair_list = []
group_flip_id_list = []
counter = 0
for i in range(len(train_flip_list)):
    names = train_flip_list[i].split('_')
    img_id = names[0]
    if i == 0:
        group_flip_id_list.append([train_flip_list[i]])
    else:
        if img_id == train_flip_list[i-1].split('_')[0]:
            group_flip_id_list[counter].append(train_flip_list[i])
        else:
            counter += 1
            group_flip_id_list.append([train_flip_list[i]])

#print (len(group_flip_id_list))
for item in group_flip_id_list:
    for i in range(len(item)):
        for j in range (i+1, len(item)):
            train_flip_pair_list.append([item[i], item[j]])

train_pair_list = train_pair_list + train_flip_pair_list

test_list = json2list('/home/yiming/code/data', 'list_test_samples.json')
group_test_id_list = []
test_pair_list = []
counter = 0
for i in range(len(test_list)):
    names = test_list[i].split('_')
    img_id = names[0]
    if i == 0:
        group_test_id_list.append([test_list[i]])
    else:
        if img_id == test_list[i-1].split('_')[0]:
            group_test_id_list[counter].append(test_list[i])
        else:
            counter += 1
            group_test_id_list.append([test_list[i]])

#print (len(group_flip_id_list))
for item in group_test_id_list:
    for i in range(len(item)):
        for j in range (i+1, len(item)):
            test_pair_list.append([item[i], item[j]])

DF_dataset_train = DeepFashionDataset(file_list = train_pair_list, mode = "train")
dataloader_train = DataLoader(DF_dataset_train, batch_size = 4, shuffle = True, num_workers = 4)
DF_dataset_test = DeepFashionDataset(file_list = test_pair_list, mode = "test")
dataloader_test = DataLoader(DF_dataset_test, batch_size = 1, shuffle = False, num_workers = 4)

#set cuda for GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

#initialize the network
U_stage1 = UnetGenerator()
U_stage1.to(device)

#set criterion and optimizer
criterion = nn.L1Loss().to(device)
optimizerL1 = torch.optim.Adam(U_stage1.parameters(), lr = 0.00002, betas = (0.5, 0.999))

#parameters for training
#train_iterations = []
train_loss_record = []
train_l1_loss_record = []
train_mask_loss_record = []
ssim_list = []
train_l1_loss = 0.0
train_mask_loss = 0.0
train_loss = 0.0
epoch_num = 5
test_id = 1
batch_size = 4

for epoch in range(epoch_num):
    print ("epoch %d" % (epoch+1))
    for i_train, train_sample in enumerate(dataloader_train):
        #print (i_batch)
        pose_mask = train_sample['pose_mask']
        pose_mask = torch.reshape(pose_mask, [batch_size, 1, 256, 256])
        pose_mask1 = pose_mask
        pose_mask2 = pose_mask
        pose_mask3 = pose_mask
        pose_mask = torch.cat((pose_mask1, pose_mask2, pose_mask3), dim = 1).to(device)
                #get inputs and groundtruth
        source_img = train_sample['source_img'].to(device)
        target_pose = train_sample['target_pose'].to(device)
        #inputs = torch.cat([source_img, target_pose], dim = 1).to(device)
        target_img = train_sample['target_img'].to(device)
        target_img_mask = target_img * pose_mask

        #outputs = U_stage1(inputs)
        outputs = U_stage1(source_img, target_pose)
        outputs_mask = outputs * pose_mask

        #computen loss and backprop
        loss1 = criterion(outputs, target_img)
        #print (loss1.data.item())
        loss2 = criterion(outputs_mask, target_img_mask)
        #print (loss2.data.item())
        loss = loss1 + loss2
        #print(loss.data.item())
        #print (loss.data.item())
        #reset optimizer
        optimizerL1.zero_grad()
        loss.backward()
        optimizerL1.step()

        train_l1_loss += loss1.data.item()
        train_mask_loss += loss2.data.item()
        train_loss += loss.data.item()

        if (i_train == 0) or (i_train+1) % 100 == 0:
            print("iteration: %d"%(i_train+1))
            if i_train == 0:
                train_l1_loss = train_l1_loss
                train_mask_loss = train_mask_loss
                train_loss = train_loss

            else:
                train_l1_loss = train_l1_loss / 100
                train_mask_loss = train_mask_loss / 100
                train_loss = train_loss / 100

            train_l1_loss_record.append(train_l1_loss)
            train_mask_loss_record.append(train_mask_loss)
            train_loss_record.append(train_loss)

            print ("train_l1_loss: %f" % train_l1_loss)
            print ("train_mask_loss: %f" % train_mask_loss)
            print ("train_loss: %f" % train_loss)
            train_l1_loss = 0.0
            train_mask_loss = 0.0
            train_loss = 0.0

        if (i_train + 1)%1000 == 0:
            #save model
            net_param_id = int((i_train + 1)/ 1000)
            torch.save(U_stage1.state_dict(), os.path.join(model_path, 'U_stage1_params' + '_' + str(epoch+1) + '_' + str(net_param_id) + '.pkl'))

            #visualize some results
            for i_test, test_sample in enumerate(dataloader_test):
                #print (i_test, test_sample['source_img'].size(), test_sample['target_img'].size(), test_sample['target_pose'].size())
                if (i_test + 1) % 30 == 0:
                    test_num = int((i_test + 1)/30)
                    print ('test %d'%test_num)

                    source_img = test_sample['source_img'].numpy()
                    source_img = np.squeeze(source_img)
                    source_img = source_img.transpose([1, 2, 0])
                    if epoch == 0 and i_train == 999:
                        io.imsave('/home/yiming/code/data/DeepFashion/DF_img_pose/stage1_mask/source_img' + str(test_num) + '.jpg', source_img)

                    target_img = test_sample['target_img'].numpy()
                    target_img = np.squeeze(target_img)
                    target_img = target_img.transpose([1, 2, 0])
                    if epoch == 0 and i_train == 999:
                        io.imsave('/home/yiming/code/data/DeepFashion/DF_img_pose/stage1_mask/target_img' + str(test_num) + '.jpg', target_img)

                    #inputs = torch.cat([test_sample['source_img'], test_sample['target_pose']], dim = 1).to(device)
                    outputs = U_stage1(test_sample['source_img'].to(device), test_sample['target_pose'].to(device)).cpu()
                    outputs_no_grad = outputs.detach()
                    generated_result = (np.squeeze(outputs_no_grad.numpy())).transpose([1, 2, 0])
                    io.imsave('/home/yiming/code/data/DeepFashion/DF_img_pose/stage1_mask/generated_result' + str(test_num) + '_iter_' + str(test_id*1000) + '.jpg', generated_result)

            print('results saved\n')
            test_id += 1

            #record ssim and loss on test data
            for i_test, test_sample in enumerate(dataloader_test):
                #ssim_result = 0.0

                target_img = test_sample['target_img'].numpy()
                target_img = np.squeeze(target_img)
                target_img = target_img.transpose([1, 2, 0])

                #inputs = torch.cat([test_sample['source_img'], test_sample['target_pose']], dim = 1).to(device)
                #outputs = U_stage1(inputs).cpu()
                outputs = U_stage1(test_sample['source_img'].to(device), test_sample['target_pose'].to(device)).cpu()

                outputs_no_grad = outputs.detach()
                generated_result = (np.squeeze(outputs_no_grad.numpy())).transpose([1, 2, 0])

                target_img_gray = rgb2gray(target_img)
                generated_result_gray = rgb2gray(generated_result)

                ssim_result += ssim(generated_result_gray, target_img_gray, data_range = target_img_gray.max() - target_img_gray.min())

            ssim_result /= (i_test + 1)
            ssim_list.append(ssim_result)
            ssim_result = 0.0
            #save record
            list2json(train_l1_loss, '/home/yiming/code/data/record/stage1_mask', 'train_l1_loss_record.json')
            train_l1_loss_record = []
            list2json(train_mask_loss, '/home/yiming/code/data/record/stage1_mask', 'train_mask_loss_record.json')
            train_mask_loss_record = []
            list2json(train_loss, '/home/yiming/code/data/record/stage1_mask', 'train_loss_record.json')
            train_loss_record = []
            list2json(ssim_list, '/home/yiming/code/data/record/stage1_mask', 'ssim_record.json')
            ssim_list = []


# -*- coding: utf-8 -*-
"""
Created on Thu Jul 23 19:58:25 2015

@author: Graham
"""

from datetime import datetime
import logging

def log_handler(msg):
    """Sends msg to the logging platform"""
    date = str(datetime.now())
    msg = date + " - " + msg
    return logging.info(msg)

def log(msg):
    """A convenience function. Adds msg to the logs with log_handler"""
    msg = str(msg)
    print("Message logged: " + msg)
    return log_handler(msg)

def addition(a, b):
    """Adds two numbers and logs the result"""
    x = a + b
    log("Adding {0} and {1} = {2}.".format(a, b, x))
    return x

addition(1, 2)
addition(2, 3)
addition(5, addition(3, 5))

Unit 1/Python Scripts/think like a coder.py


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('cycleoholic', '0005_auto_20150403_1741'),
    ]

    operations = [
        migrations.CreateModel(
            name='Store',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('store_address', models.CharField(max_length=200)),
                ('store_contact', models.CharField(max_length=15)),
                ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.RemoveField(
            model_name='client',
            name='user',
        ),
        migrations.AlterField(
            model_name='product',
            name='store_owns',
            field=models.ForeignKey(to='cycleoholic.Store'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='purchase',
            name='purchased_from',
            field=models.ForeignKey(to='cycleoholic.Store'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='sell',
            name='seller',
            field=models.ForeignKey(to='cycleoholic.Store'),
            preserve_default=True,
        ),
        migrations.DeleteModel(
            name='Client',
        ),
    ]


#!/usr/bin/python3

# -*- Python -*-
#
#*****************************************************************
#
# hackerrank: https://www.hackerrank.com/challenges/text-wrap/problem
#
# WARRANTY:
# Use all material in this file at your own risk.
#
# Created by Hiranmoy Basak on 18/05/18.
#
#*****************************************************************
#

import textwrap

def wrap(string, max_width):
  wrapped_str = ""

  wrap_strs = textwrap.wrap(string, width=max_width)
  for x in wrap_strs:
    wrapped_str = wrapped_str+x+'\n'
  return wrapped_str

if __name__ == '__main__':
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.float_format = '{:,.2f}'.format
pd.set_option('display.max_rows', 200)

# The answers for each question are in the bottom part of this code.
data_population = pd.read_csv("data/population/API_SP.POP.TOTL_DS2_en_csv_v2_382278.csv", skiprows=[0,2])
data_gdp = pd.read_csv("data/gdp/API_NY.GDP.MKTP.CD_DS2_en_csv_v2_383060.csv", skiprows=[0,2])
meta_gdp_country = pd.read_csv("data/gdp/Metadata_Country_API_NY.GDP.MKTP.CD_DS2_en_csv_v2_383060.csv")

# Q1: Retrieve GDP and Population data for Bangladesh in year 1975.
gdp_bangladesh = data_gdp[data_gdp["Country Name"] == "Bangladesh"]["1975"].values[0]
pop_bangladesh = data_population[data_population["Country Name"] == "Bangladesh"]["1975"].values[0]

# Q2: Filter all countries starting with the letter "B" and retrieve their GDPs for year 1999.
b_countries = [country for country in data_gdp["Country Name"] if country[0] == "B"]
gdp_b_countries = [data_gdp[data_gdp["Country Name"] == country]["1999"].values[0] for country in b_countries]

# Q3: Retrieve all GDP and Population year columns for Iran excluding years with null values and return the years they have in common.
gdp_iran = data_gdp[data_gdp["Country Name"] == "Iran, Islamic Rep."].iloc[:, 4:-1].notnull()
gdp_iran_complete = [col for col, val in gdp_iran.items() if val.values[0] == True]
pop_iran = data_population[data_population["Country Name"] == "Iran, Islamic Rep."].iloc[:, 4:-1].notnull()
pop_iran_complete = [col for col, val in gdp_iran.items() if val.values[0] == True]
iran_complete = sorted(list(set(gdp_iran_complete).intersection(set(pop_iran_complete))))

# Q4: Compute the percentage of growth from 1984 to 1985 and return the country with the largest growth.
data_population["YoY"] = ((data_population["1985"] - data_population["1984"]) / data_population["1984"]) * 100
highest_yoy = data_population.nlargest(1, ["YoY"])["Country Name"].values[0]

# Q5: Compute the percentage of growth from 2007 to 2012, get the top three, and return the third value.
data_population["5 Year Growth"] = ((data_population["2012"] - data_population["2007"]) / data_population["2007"]) * 100
third_highest_yoy = data_population.nlargest(3, ["5 Year Growth"])["Country Name"].values[2]

# Q6: Merge GDP data and GDP region metadata and filter them by region and year 2000.
merged_gdp = meta_gdp_country.set_index("Country Code").join(data_gdp.set_index("Country Code"))
latin_caribbean = merged_gdp[merged_gdp["Region"] == "Latin America & Caribbean"]["2000"]
south_asia = merged_gdp[merged_gdp["Region"] == "South Asia"]["2000"]
subsaharan_africa = merged_gdp[merged_gdp["Region"] == "Sub-Saharan Africa"]["2000"]
europe_central_asia = merged_gdp[merged_gdp["Region"] == "Europe & Central Asia"]["2000"]
mideast_north_africa = merged_gdp[merged_gdp["Region"] == "Middle East & North Africa"]["2000"]
east_asia_pacific = merged_gdp[merged_gdp["Region"] == "East Asia & Pacific"]["2000"]
north_america = merged_gdp[merged_gdp["Region"] == "North America"]["2000"]

# Q7: Merge GDP data plus GDP region metadata with Population data.
merged_pop_gdp = data_population.merge(merged_gdp, left_on="Country Name", right_on="Country Name", suffixes=("_pop", "_gdp"))
merged_north_america = merged_pop_gdp[merged_pop_gdp["Region"] == "North America"].copy()

# Calculate the GDP Per Capita for all North American countries from years 1965 onwards.
for year in range(1960, 2020):
    try:
        gdp_per_capita = merged_north_america[str(year) + "_gdp"] / merged_north_america[str(year) + "_pop"]
        merged_north_america["GDP Per Capita " + str(year)] = gdp_per_capita
    except KeyError:
        pass

# Retrieves columns GDP Per Capita from years 1960 to 1964 [1960, 1961, 1962, 1963, 1964] = 5 Years, 1965 to 1969, and so on.
for year in range(1960, 2020):
    try:
        gdp_cols = ["GDP Per Capita " + str(year) for year in range(year, year + 5)]
        merged_north_america["5 Year RA GDP " + str(year + 4)] = round(np.nanmean(merged_north_america[gdp_cols]), 2)
    except KeyError:
        pass

# Retrieves all 5 year rolling average of GDP per capita in North America.
all_gdp_cols = [col for col in merged_north_america.columns if "5 Year RA GDP" in col]
north_america = merged_north_america[all_gdp_cols]

# Q8: Retrieves all Income Group values for 2016 and computes the min, max, and 60th percentile for each.
high_income = merged_gdp[merged_gdp["IncomeGroup"] == "High income"]
low_income = merged_gdp[merged_gdp["IncomeGroup"] == "Low income"]
lowmid_income = merged_gdp[merged_gdp["IncomeGroup"] == "Lower middle income"]
upmid_income = merged_gdp[merged_gdp["IncomeGroup"] == "Upper middle income"]

high_income_min = high_income.nsmallest(1, ["2016"])["2016"].values[0]
high_income_max = high_income.nlargest(1, ["2016"])["2016"].values[0]
high_income_60th = high_income["2016"].quantile(0.6)

low_income_min = low_income.nsmallest(1, ["2016"])["2016"].values[0]
low_income_max = low_income.nlargest(1, ["2016"])["2016"].values[0]
low_income_60th = low_income["2016"].quantile(0.6)

lowmid_income_min = lowmid_income.nsmallest(1, ["2016"])["2016"].values[0]
lowmid_income_max = lowmid_income.nlargest(1, ["2016"])["2016"].values[0]
lowmid_income_60th = lowmid_income["2016"].quantile(0.6)

upmid_income_min = upmid_income.nsmallest(1, ["2016"])["2016"].values[0]
upmid_income_max = upmid_income.nlargest(1, ["2016"])["2016"].values[0]
upmid_income_60th = upmid_income["2016"].quantile(0.6)

gdp_income_groups = pd.DataFrame({"Income Group": ["High Income", "Low Income", "Lower Middle Income", "Upper Middle Income"], \
                                  "Minimum": [high_income_min, low_income_min, lowmid_income_min, upmid_income_min], \
                                  "Maximum": [high_income_max, low_income_max, lowmid_income_max, upmid_income_max], \
                                  "60th Percentile": [high_income_60th, low_income_60th, lowmid_income_60th, upmid_income_60th]})

print("1. What was the GDP per capita of Bangladesh in 1975?\n", round(gdp_bangladesh / pop_bangladesh, 2))
print("\n2. What is the average GDP of all countries that start with the letter B in 1999?\n", round(np.nanmean(gdp_b_countries), 2))
print("\n3. What years do we have complete data (GDP and Population) for Iran?\n", ", ".join(iran_complete))
print("\n4. Which country had the highest YoY population growth in 1985?\n", highest_yoy)
print("\n5. Which county had the 3rd highest 5 year population growth in 2012?\n", third_highest_yoy)
print("\n6. What was the average GDP per capita of each region (for reference, the regions are below) in 2000?")
print("\ta. Latin America & Caribbean - ", round(np.nanmean(latin_caribbean), 2))
print("\tb. South Asia - ", round(np.nanmean(south_asia), 2))
print("\tc. Sub-Saharan Africa - ", round(np.nanmean(subsaharan_africa), 2))
print("\td. Europe & Central Asia - ", round(np.nanmean(europe_central_asia), 2))
print("\te. Middle East & North Africa - ", round(np.nanmean(mideast_north_africa), 2))
print("\tf. East Asia & Pacific - ", round(np.nanmean(east_asia_pacific), 2))
print("\tg. North America - ", round(np.nanmean(north_america), 2))
print("\n7. Plot the 5 year rolling average of GDP per capita in North America from 1965 to the current year.", north_america.iloc[0])
print("\n8. What is the minimum, maximum and 60th percentile GDP of each of the income groups (for reference, the income groups are below) in 2016?\n")
print(gdp_income_groups)


import jinja2

# from pyramid.authentication import AuthTktAuthenticationPolicy
# from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.config import Configurator

from chitragupta.models.meta.base import (
    DBSession,
    Base,
)

from chitragupta.settings import MEDIA_ROOT, TEMPLATE_ROOT
from chitragupta.settings.database import get_db_engine
# from chitragupta.settings.security import AUTH_SECRET
# from chitragupta.security import (
#     get_principal_indentifiers,
#     RootFactory,
# )
from chitragupta.jinja2_filters import add_filters_to_jinja2_env

template_loader = jinja2.FileSystemLoader(TEMPLATE_ROOT)
JINJA2_ENV = jinja2.Environment(
    loader=template_loader, extensions=['jinja2.ext.with_'])

add_filters_to_jinja2_env(JINJA2_ENV)

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = get_db_engine()
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine

    # authentication_policy = AuthTktAuthenticationPolicy(
    #     AUTH_SECRET,
    #     callback=get_principal_indentifiers,
    # )
    # authorization_policy = ACLAuthorizationPolicy()

    config = Configurator(
        settings=settings,
    )

    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_static_view(name='media', path=MEDIA_ROOT, cache_max_age=3600)

    # Add .html extension as a renderer
    config.add_jinja2_renderer('.html')

    # Scan views for route configuration
    config.include('chitragupta.views.home')

    config.scan()

    return config.make_wsgi_app()


# Author: Zylo117
# Modified by S.Y

import argparse
import os
import time
from pathlib import Path

import torch
import yaml
from torch.backends import cudnn
from tqdm import tqdm

from efficientdet.backbone import EfficientDetBackbone
import cv2
import numpy as np

from efficientdet.utils import BBoxTransform, ClipBoxes
from utils.utils import preprocess, invert_affine, postprocess, STANDARD_COLORS, standard_to_bgr, get_index_label, plot_one_box

def get_args():
    ap = argparse.ArgumentParser()
    ap.add_argument('-p', '--project', type=str, default='nuScenes',
                    help='project file that contains parameters')
    ap.add_argument("--mode", help="mode for prediction. 0: single picture, 1: folder", type=int, default=1)
    ap.add_argument("--img_dir", help="image dir to be detected", type=str, default="/home/yang/centerpoint_maps/data/nuScenes/samples")
    ap.add_argument('-w', '--weights', type=str, default="/home/yang/mmdetection/efficientDet/logs/nuScenes/efficientdet-d1_48_22400.pth",
                    help='path of weights file')
    args = ap.parse_args()
    return args

class Params:  # load config file
    def __init__(self, project_file):
        self.params = yaml.safe_load(open(project_file).read())

    def __getattr__(self, item):
        return self.params.get(item, None)

def generate_proposals(preds, img_path, sub_out_dir):

    # get image name
    img = img_path.split('/')[-1]
    frame_id = img.split('.')[0]
    out_file = os.path.join(sub_out_dir, (frame_id + '.txt'))

    bboxes = []
    scores = []
    objs = []
    for j in range(len(preds['rois'])):
        x1, y1, x2, y2 = preds['rois'][j]
        obj = obj_list[preds['class_ids'][j]]
        score = float(preds['scores'][j])
        bboxes.append([x1, y1, x2, y2])
        scores.append(score)
        objs.append(obj)

        context = []
        for i in range(len(objs)):
            if (objs[i] == 'car') & (len(context) < 200):  # save 200 highest proposals
                bbox = bboxes[i]
                if ((i + 1) < len(objs)) & (len(context) < 199):
                    write_line = "Car -1 -1 -10 {:.2f} {:.2f} {:.2f} {:.2f} -1 -1 -1 -1000 -1000 -1000 -10 {:.4f} \n". \
                        format(bbox[0], bbox[1], bbox[2], bbox[3], scores[i])
                else:  # if the last line
                    write_line = "Car -1 -1 -10 {:.2f} {:.2f} {:.2f} {:.2f} -1 -1 -1 -1000 -1000 -1000 -10 {:.4f} ". \
                        format(bbox[0], bbox[1], bbox[2], bbox[3], scores[i])

                context.append(write_line)
        # write all 2d proposals
        with open(out_file, 'w+') as f:
            for line in context:
                f.write(line)

if __name__ == '__main__':
    # get args
    args = get_args()
    project_name = args.project
    mode = args.mode
    img_dir = args.img_dir

    # load config file
    params = Params(f'config/{project_name}.yaml')
    compound_coef = params.compound_coef
    nms_threshold = float(params.nms_threshold)
    use_cuda = bool(params.cuda)
    gpu = params.device
    cudnn.fastest = True
    cudnn.benchmark = True
    use_float16 = bool(params.float16)
    extreme_fps_test = bool(params.extreme_fps_test)
    obj_list = params.obj_list
    force_input_size = params.force_input_size
    weights_path = f'weights/efficientdet-d{compound_coef}.pth' if args.weights is None else args.weights
    anchor_ratios = eval(params.anchors_ratios)
    anchor_scales = eval(params.anchors_scales)
    threshold = params.score_threshold
    iou_threshold = params.iou_threshold
    out_dir = "Output_2D"

    channel_list = ['CAM_BACK', 'CAM_BACK_LEFT', 'CAM_BACK_RIGHT', 'CAM_FRONT', 'CAM_FRONT_LEFT', 'CAM_FRONT_RIGHT']
    for ref_chan in channel_list:
        sub_img_dir = os.path.join(img_dir, ref_chan)
        sub_out_dir = os.path.join(out_dir, ref_chan)
        # create output dir
        Path(sub_out_dir).mkdir(parents=True, exist_ok=True)
        image_path = os.listdir(sub_img_dir)
        img_paths = [os.path.join(sub_img_dir, img_path) for img_path in image_path]
        color_list = standard_to_bgr(STANDARD_COLORS)

        # tf bilinear interpolation is different from any other's, just make do
        input_sizes = eval(params.input_sizes)
        input_size = input_sizes[compound_coef] if force_input_size is 0 else force_input_size

        model = EfficientDetBackbone(compound_coef=compound_coef, num_classes=len(obj_list),
                                     ratios=anchor_ratios, scales=anchor_scales)
        model.load_state_dict(torch.load(weights_path, map_location='cpu'))
        model.requires_grad_(False)
        model.eval()

        if use_cuda:
            model = model.cuda()
        if use_float16:
            model = model.half()

        with tqdm(total=len(img_paths)) as pbar:
            for img_path in img_paths:
                ori_imgs, framed_imgs, framed_metas = preprocess(img_path, max_size=input_size)

                if use_cuda:
                    x = torch.stack([torch.from_numpy(fi).cuda() for fi in framed_imgs], 0)
                else:
                    x = torch.stack([torch.from_numpy(fi) for fi in framed_imgs], 0)

                x = x.to(torch.float32 if not use_float16 else torch.float16).permute(0, 3, 1, 2)

                with torch.no_grad():
                    features, regression, classification, anchors = model(x)

                regressBoxes = BBoxTransform()
                clipBoxes = ClipBoxes()

                out = postprocess(x,
                                  anchors, regression, classification,
                                  regressBoxes, clipBoxes,
                                  threshold, iou_threshold)

                out = invert_affine(framed_metas, out)

                if len(out[0]['rois']) == 0:
                    continue
                generate_proposals(out[0], img_path, sub_out_dir)
                pbar.update(1)


#coding:utf-8

from tbd import msrun
import re

def stockQuantity():
    specilString = "'%Y-%m-%d'"
    _rt_quantity = [{'id': '0.0','parent': '','name': '全部库存'}]
    sql_rt_date = 'SELECT date_format(stock_date,%s) as date from stock order by stock_date desc limit 1'%(specilString)
    _rt_date = msrun.Query(sql_rt_date)[0]['date']
    sql_platform_list = 'SELECT platform_id as id, %s as parent,platform as name From primary_warec GROUP BY 1,3'%("'%s'" %('0.0'))
    _rt_platform_list = msrun.Query(sql_platform_list)
    for i in _rt_platform_list:
        _rt_quantity.append(i)
    sql_area_list = 'SELECT area_id as id,platform_id as parent,area as name FROM primary_warec GROUP BY 1,2,3'
    _rt_area_list = msrun.Query(sql_area_list)
    for i in _rt_area_list:
        _rt_quantity.append(i)
    sql_warehouse_list = 'SELECT CONCAT(%s,a.wh_id) as id,a.area_id as parent,a.warehouse as name,b.quantity as value FROM primary_warec a,(SELECT warehouse,platform,sum(quantity_stock)as quantity from stock GROUP BY warehouse,platform) b where a.warehouse=b.warehouse and a.platform=b.platform GROUP BY 1,2,3' %("'%s'" %('3.'))
    _rt_warehouse_list = msrun.Query(sql_warehouse_list)
    for i in _rt_warehouse_list:
        i['value'] = int(i['value'])
        _rt_quantity.append(i)
    print(_rt_quantity)
    return _rt_quantity

def salesAmount_Month(num):
    specilString = "'%Y%m'"
    sql_sales = 'select date_format(sales_date,%s) as month,\
    sum(quantity) as quantity from sales group by 1 order by 1 desc limit %s' %(specilString,int(num))
    _rt_sales = msrun.Query(sql_sales)
    q_sales_list = []
    month_list = []
    for i in _rt_sales:
        month_list.append(i['month'])
        q_sales_list.append(int(i['quantity']))
    return q_sales_list,month_list

def stockQuantity_Day(num):
    specilString = "'%Y%m%d'"
    sql_stock = 'select date_format(stock_date,%s) as day,platform,\
    sum(quantity_stock) as quantity from stock group by 1,2 order by 3 desc limit %d ' %(specilString,int(num))
    _rt_stock = msrun.Query(sql_stock)
    platform_list = []
    q_stock_list = []
    day_list = []
    for i in _rt_stock:
        platform_list.append(i['platform'])
        q_stock_list.append(i['quantity'])
        day_list.append(i['day'])
    return platform_list,q_stock_list,day_list

def averageSale(_day_list):
    specilString = "'%Y%m%d'"
    sql_average_sales = 'select date_format(sales_date,%s) as date,productid,avg(quantity) \
    as avg_quantity from sales where  date_format(sales_date,%s) in "%s" \
    group by 1,2 order by 1,2' %(specilString,specilString,specilString,','.join(_day_list))
    _average_sales = msrun(sql_average_sales)
    _rt_daylist = []
    _rt_producilist = []
    _rt_avg_quantity = []
    for i in _average_sales:
        _rt_daylist.append(i['date'])
        _rt_producilist.append(i['productid'])
        _rt_avg_quantity.append(i['avg_quantity'])
    return _rt_daylist,_rt_producilist,_rt_avg_quantity

def dayChooseNum(num):
    specilString = "'%Y%m%d'"
    sql_choose_number = 'select date_format(sales_date,%s) from sales group by 1 order by 1 desc limit %d' %(num)
    _choose_daylist = msrun.Query(sql_choose_number)
    _rt_daylist = []
    for i in _choose_daylist:
        _rt_daylist.append(i)
    return _rt_daylist

def jdb2bServer(num):
    specilString = "'%Y%m%d'"
    sql_jdb2b_server = 'select date_format(date,%s) as date,sum(application_quantity) as app_q,\
    sum(app_finished_quantity) as app_f_q,sum(receive_quantity) as rec_q,sum(rece_finished_quantity) as rec_f_q,\
    sum(evaluate_quantity) as eva_q,sum(eva_good_quantity) as eva_g_q \
    from service_score_jdb2b group by 1 order by 1 desc limit %s'%(specilString,num)
    _jdb2b_server = msrun.Query(sql_jdb2b_server)
    _rt_daylist = []
    _rt_ratio_app = []
    _rt_ratio_rec = []
    _rt_ratio_eva = []
    for i in _jdb2b_server:
        _rt_daylist.append(i['date'])
        _rt_ratio_app.append(float('%.4f'%(i['app_f_q']/i['app_q'])))
        _rt_ratio_rec.append(float('%.4f'%(i['rec_f_q']/i['rec_q'])))
        _rt_ratio_eva.append(float('%.4f'%(i['eva_g_q']/i['eva_q'])))
    _rt_daylist.reverse()
    _rt_ratio_app.reverse()
    _rt_ratio_rec.reverse()
    _rt_ratio_eva.reverse()
    return _rt_daylist,_rt_ratio_app,_rt_ratio_rec,_rt_ratio_eva

def jdfbpServerApp(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_app = 'select date_format(date,%s) as date,sum(app_q) as app_q,\
    sum(app_audit_q) as app_audit_q,sum(app_audit_p_q) as app_audit_p_q from service_score_jdfbp \
    group by 1 order by 1 desc limit %s' %(specilString,num)
    _jdfbp_app = msrun.Query(sql_jdfbp_app)
    _rt_day = []
    _rt_app = []
    _rt_app_a = []
    _rt_app_a_p = []
    for i in _jdfbp_app:
        _rt_day.append(i['date'])
        _rt_app.append(int(i['app_q']))
        _rt_app_a.append(int(i['app_audit_q']))
        _rt_app_a_p.append(int(i['app_audit_p_q']))
    _rt_day.reverse()
    _rt_app.reverse()
    _rt_app_a.reverse()
    _rt_app_a_p.reverse()
    return _rt_day,_rt_app,_rt_app_a,_rt_app_a_p

def jdfbpServerEva(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_eva = 'select date_format(date,%s) as date,sum(eva_q) as eva_q,\
    sum(eva_good_q) as eva_good_q,sum(eva_bad_q) as eva_bad_q,\
    sum(eva_normal_q) as eva_normal_q from service_score_jdfbp group by 1 order by 1 desc limit %s' %(specilString,num)
    _jdfbp_eva = msrun.Query(sql_jdfbp_eva)
    _rt_day = []
    _rt_eva = []
    _rt_eva_g = []
    _rt_eva_b = []
    _rt_eva_n = []
    for i in _jdfbp_eva:
        _rt_day.append(i['date'])
        _rt_eva.append(int(i['eva_q']))
        _rt_eva_g.append(int(i['eva_good_q']))
        _rt_eva_b.append(int(i['eva_bad_q']))
        _rt_eva_n.append(int(i['eva_normal_q']))
    _rt_day.reverse()
    _rt_eva.reverse()
    _rt_eva_g.reverse()
    _rt_eva_b.reverse()
    _rt_eva_n.reverse()
    return _rt_day,_rt_eva,_rt_eva_g,_rt_eva_b,_rt_eva_n

def jdfbpServerStatis(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_satis = 'select date_format(date,%s) as date,\
    sum(degree_satis) as degree_satis from service_score_jdfbp group by 1 order by 1 desc limit %s' %(specilString,num)
    print(sql_jdfbp_satis)
    _jdfbp_satis = msrun.Query(sql_jdfbp_satis)
    _rt_day = []
    _rt_satis = []
    for i in _jdfbp_satis:
        _rt_day.append(i['date'])
        _rt_satis.append(int(i['degree_satis']))
    _rt_day.reverse()
    _rt_satis.reverse()
    return _rt_day,_rt_satis

def jdfbpServerAuditTime(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_AuditTime = 'select date_format(date,%s) as date,\
    sum(avg_audit_time) as avg_audit_time from service_score_jdfbp group by 1 order by 1 desc limit %s' %(specilString,num)

    _jdfbp_audittime = msrun.Query(sql_jdfbp_AuditTime)
    _rt_day = []
    _rt_audittime = []
    for i in _jdfbp_audittime:
        _rt_day.append(i['date'])
        _rt_audittime.append(float('%.2f'%(i['avg_audit_time'])))
    _rt_day.reverse()
    _rt_audittime.reverse()
    return _rt_day,_rt_audittime

def jdfbpServerDisposeTime(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_DisposeTime = 'select date_format(date,%s) as date,\
    sum(avg_dispose_time) as avg_dispose_time from service_score_jdfbp group by 1 order by 1 desc limit %s' %(specilString,num)

    _jdfbp_disposetime = msrun.Query(sql_jdfbp_DisposeTime)
    _rt_day = []
    _rt_disposetime = []
    for i in _jdfbp_disposetime:
        _rt_day.append(i['date'])
        _rt_disposetime.append(float('%.2f'%(i['avg_dispose_time'])))
    _rt_day.reverse()
    _rt_disposetime.reverse()
    return _rt_day,_rt_disposetime

def jdfbpServerDSR(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_DSR = 'select date_format(date,%s) as date,\
    sum(grade_dsr) as grade_dsr from service_score_jdfbp group by 1 order by 1 desc limit %s' %(specilString,num)

    _jdfbp_dsr = msrun.Query(sql_jdfbp_DSR)
    _rt_day = []
    _rt_dsr = []
    for i in _jdfbp_dsr:
        _rt_day.append(i['date'])
        _rt_dsr.append(float('%.2f'%(i['grade_dsr'])))
    _rt_day.reverse()
    _rt_dsr.reverse()
    return _rt_day,_rt_dsr

def jdfbpServerDispute(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_dispute = 'select date_format(date,%s) as date,\
    sum(ratio_dispute) as ratio_dispute from service_score_jdfbp group by 1 order by 1 desc limit %s' %(specilString,num)
    _jdfbp_dispute = msrun.Query(sql_jdfbp_dispute)
    _rt_day = []
    _rt_dispute = []
    for i in _jdfbp_dispute:
        _rt_day.append(i['date'])
        _rt_dispute.append(float('%.2f'%(i['ratio_dispute'])))
    _rt_day.reverse()
    _rt_dispute.reverse()
    return _rt_day,_rt_dispute

def jdfbpServerIndustry(num):
    specilString = "'%Y%m%d'"
    sql_jdfbp_industry = 'select date_format(date,%s) as date,\
    sum(ratio_server) as ratio_server,sum(ratio_industry) as ratio_industry from service_score_jdfbp group by 1 order by 1 desc limit %s' %(specilString,num)
    _jdfbp_industry = msrun.Query(sql_jdfbp_industry)
    _rt_day = []
    _rt_myserver = []
    _rt_industry = []
    for i in _jdfbp_industry:
        _rt_day.append(i['date'])
        _rt_myserver.append(float('%.2f'%(i['ratio_server'])))
        _rt_industry.append(float('%.2f'%(i['ratio_industry'])))
    _rt_day.reverse()
    _rt_myserver.reverse()
    _rt_industry.reverse()
    return _rt_day,_rt_myserver,_rt_industry

#计算中央维修量（天维度，型号占比）
def centreRepairQuantity(num):
    specilString = "'%Y-%m-%d'"
    sql_cr_quantity = 'select date_format(finished_time,%s) as date,count(id) as num from centre_repair_data group by 1 order by 1 desc limit %s'%(specilString,num)
    _rt_cr_quantity = msrun.Query(sql_cr_quantity)
    _rt_date = []
    _rt_quantity = []
    for i in _rt_cr_quantity:
        _rt_date.append(i['date'])
        _rt_quantity.append(i['num'])
    sql_cr_product_q = 'select product,sum(warranty_quantity) as num from centre_repair_data where date_format(finished_time,%s) in (%s) group by 1 order by 2 desc'%(specilString,','.join('"%s"'% j for j in _rt_date))
    _rt_cr_product_q = msrun.Query(sql_cr_product_q)
    #循环产品列表，然后合并合计数量
    _rt_product_dic = {}
    for i in _rt_cr_product_q:
        if re.findall(r'[WATDPSVGF][A-Za-z0-9-]+',i['product']):
            product = str(re.findall(r'[WATDPSVGF][A-Za-z0-9-]+',i['product'])[0])
            _rt_num = int(i['num'])
            if product not in _rt_product_dic.keys():
                _rt_product_dic[product]=_rt_num
            else:
                _rt_product_dic[product] = _rt_product_dic[product] + _rt_num
        else:
            pass
    _rt_product = []
    for i in _rt_product_dic:
        temp = []
        temp.append(i)
        temp.append(_rt_product_dic[i])
        _rt_product.append(temp)
    sql_repeate_cause = 'select operate_status,count(id) as repeate_num from centre_repair_data where date_format(finished_time,%s) in (%s)  and operate_status in (1,2,3,4,9) group by 1 order by 2 desc'%(specilString,','.join('"%s"'% j for j in _rt_date))
    _rt_repeate_cause = msrun.Query(sql_repeate_cause)
    repeate_cause_list = []
    for i in _rt_repeate_cause:
        temp = []
        temp.append(i['operate_status'])
        temp.append(i['repeate_num'])
        repeate_cause_list.append(temp)
    _rt_repeate_cause_list = []
    status_field = {1:'技术',2:'产品',3:'客服',4:'其他',9:'未处理'}
    for i in repeate_cause_list:
        i[0] = status_field[i[0]]
        _rt_repeate_cause_list.append(i)

    #三十天维修型号的饼形图
    p_dic = {'sliced': 'true','selected': 'true','name':0,'y':0}#饼形图必须的一个属性项目
    temp = _rt_product.pop()
    p_dic['name'],p_dic['y'] = temp[0],temp[1]
    _rt_product.append(p_dic)
    #三十天二次维修原因饼形图
    cause_dic = {'sliced': 'true','selected': 'true','name':0,'y':0}#饼形图必须的一个属性项目
    temp_repeate_cause = _rt_repeate_cause_list.pop()
    cause_dic['name'],cause_dic['y'] = temp_repeate_cause[0],temp_repeate_cause[1]
    _rt_repeate_cause_list.append(cause_dic)
    print(_rt_repeate_cause_list)
    _rt_date.reverse()
    _rt_quantity.reverse()
    _rt_product.reverse()
    return _rt_date,_rt_quantity,_rt_product,_rt_repeate_cause_list

def centreRepairRepeate(num):
    #直接取二次维修率的生成表单
    sql_repair_repeate = 'SELECT date_format(date,%s) as date,total_num,repeat_num FROM centre_repair_repeat GROUP BY 1 ORDER BY 1 desc limit %s'%("'%s'"%('%Y-%m-%d'),num)
    _rt_repair_repeate = msrun.Query(sql_repair_repeate)
    _rt_date_p = []
    _rt_ratio_rep = []
    _rt_repeate_num = []
    for i in _rt_repair_repeate:
        _rt_date_p.append(i['date'])
        _rt_repeate_num.append(i['repeat_num'])
        _rt_ratio_rep.append(float('%.4f'%(i['repeat_num']/i['total_num'])))
    print(_rt_ratio_rep)
    _rt_date_p.reverse()
    _rt_ratio_rep.reverse()
    _rt_repeate_num.reverse()

    return _rt_date_p,_rt_ratio_rep,_rt_repeate_num


#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

from django.conf.urls import url
from blog import views

urlpatterns = [

    # 修改密码页
    url(r'chgpwd/', views.change_pwd, name='change_pwd'),
    # 博客文章详情页
    url(r'^(\w+)/article/(\d+)/', views.article_detail, name='article_detail'),
    # 博客文章按分类展示
    url(r'^(\w+)/category/(\d+)/', views.article_category, name='article_category'),
    # 博客文章按标签展示
    url(r'^(\w+)/tag/(\d+)/', views.article_tag, name='article_tag'),
    # 博客文章按日期展示
    url(r'^(\w+)/archive/(\d+)/(\d+)/', views.article_archive, name='article_archive'),
    # 点赞或点踩
    url(r'^up_down/', views.up_down, name='up_down'),
    # 评论文章
    url(r'^comment/', views.comment, name='comment'),
    # 编辑个人信息
    url(r'^personalinfo/edit/', views.edit_info, name='edit_info'),
    # 展示个人信息
    url(r'^personalinfo/', views.personal_info, name='user_info'),
    # 个人博客主页
    url(r'^(\w+)/', views.home, name='user_home'),

]


import geomstats.backend as gs
from geomstats.algebra_utils import from_vector_to_diagonal_matrix
from geomstats.geometry.skew_symmetric_matrices import SkewSymmetricMatrices
from geomstats.geometry.special_euclidean import SpecialEuclidean
from geomstats.geometry.special_orthogonal import SpecialOrthogonal
from tests.data_generation import TestData

class TestDataLieAlgebra(TestData):
    def dimension_test_data(self):
        smoke_data = [dict(algebra=SkewSymmetricMatrices(4), expected=6)]
        return self.generate_tests(smoke_data)

    def matrix_representation_and_belongs_test_data(self):
        smoke_data = [
            dict(algebra=SkewSymmetricMatrices(4), point=gs.random.rand(2, 6))
        ]
        return self.generate_tests(smoke_data)

    def orthonormal_basis_test_data(self):
        smoke_data = [
            dict(group=SpecialOrthogonal(3, equip=False), metric_mat_at_identity=None),
            dict(
                group=SpecialOrthogonal(3, equip=False),
                metric_mat_at_identity=from_vector_to_diagonal_matrix(
                    gs.array([1.0, 2.0, 3.0])
                ),
            ),
        ]
        return self.generate_tests(smoke_data)

    def orthonormal_basis_se3_test_data(self):
        smoke_data = [
            dict(group=SpecialEuclidean(3, equip=False), metric_mat_at_identity=None),
            dict(
                group=SpecialEuclidean(3, equip=False),
                metric_mat_at_identity=from_vector_to_diagonal_matrix(
                    gs.cast(
                        gs.arange(1, SpecialEuclidean(3, equip=False).dim + 1),
                        gs.float32,
                    )
                ),
            ),
        ]
        return self.generate_tests(smoke_data)