"""File paths for the Reddit Classification pipeline.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

TRANSFORM_FN_DIR = 'transform_fn'
RAW_METADATA_DIR = 'raw_metadata'
TRANSFORMED_METADATA_DIR = 'transformed_metadata'
TRANSFORMED_TRAIN_DATA_FILE_PREFIX = 'features_train'
TRANSFORMED_EVAL_DATA_FILE_PREFIX = 'features_eval'
TRANSFORMED_PREDICT_DATA_FILE_PREFIX = 'features_predict'
TRAIN_RESULTS_FILE = 'train_results'
DEPLOY_SAVED_MODEL_DIR = 'saved_model'
MODEL_DIR = 'model_dir'
MODEL_EVALUATIONS_FILE = 'model_evaluations'
BATCH_PREDICTION_RESULTS_FILE = 'batch_prediction_results'


#!/usr/bin/env python

# Copyright (c) 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging
import unittest

from systrace import decorators
from systrace import run_systrace
from systrace.tracing_agents import walt_agent

class WaltAgentTest(unittest.TestCase):
  """
  The WALT agent pulls the trace log from the Android phone, and does not
  communicate with the WALT device directly. This makes the agent similar
  to atrace. Since the host only connects to the Android phone, more exhaustive
  testing would require mocking DeviceUtils.
  """

  @decorators.HostOnlyTest
  def test_construct_walt_args(self):
    options, _ = run_systrace.parse_options(['./run_systrace.py',
                                                      '--walt'])
    self.assertTrue(walt_agent.get_config(options).is_walt_enabled)
    options, _ = run_systrace.parse_options(['./run_systrace.py'])
    self.assertFalse(walt_agent.get_config(options).is_walt_enabled)

  @decorators.HostOnlyTest
  def test_format_clock_sync_marker(self):
    actual_marker = walt_agent.format_clock_sync_marker(
                    'some_sync_id', 12345678901234)
    expected_marker = ('<0>-0  (-----) [001] ...1  12345.6789012: ' +
                       'tracing_mark_write: trace_event_clock_sync: ' +
                       'name=some_sync_id\n')
    self.assertEqual(actual_marker, expected_marker)

  @decorators.HostOnlyTest
  def test_get_results_string(self):
    agent = walt_agent.WaltAgent()
    agent._trace_contents = '<trace contents here>\n'
    agent._clock_sync_marker = '<clock sync marker here>\n'
    result = agent._get_trace_result()
    self.assertEquals(result, '# tracer: \n# clock_type=LINUX_CLOCK_MONOTONIC\n'
                      '<trace contents here>\n<clock sync marker here>\n')

if __name__ == "__main__":
  logging.getLogger().setLevel(logging.DEBUG)
  unittest.main(verbosity=2)


import argparse
import tensorflow as tf
import sys
import os.path
import os
tf.reset_default_graph()
os.environ['CUDA_VISIBLE_DEVICES'] = '0'

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from deep_cpl import Deep_CPL
from my_utils import get_pairwise_train_dataset, get_test_data
from time import time

def parse_args():
    parser = argparse.ArgumentParser(description='DeepRec')
    parser.add_argument('--model',
                        default='Deep_CPL')

    parser.add_argument('--dataset',
                        default='ml100k')
    parser.add_argument('--learning_rate', type=float, default=0.5)
    parser.add_argument('--reg_rate', type=float, default=0.001)
    parser.add_argument('--margin', type=float, default=1.9)
    parser.add_argument('--epochs', type=int, default=100)
    parser.add_argument('--batch_size', type=int, default=1024)
    parser.add_argument('--verbose', type=bool, default=False)
    parser.add_argument('--t', type=int, default=1)
    parser.add_argument('--display_step', type=int, default=1000)
    parser.add_argument('--num_factor', type=int, default=10)
    parser.add_argument('--num_factor_mlp', type=int, default=64)
    parser.add_argument('--hidden_dimension', type=int, default=10)
    parser.add_argument('--topK', type=int, default=10)
    parser.add_argument('--lr_decay_rate', type=float, default=0.85)
    parser.add_argument('--train_n', type=int, default=50)
    parser.add_argument('--test_n', type=int, default=70)

    return parser.parse_args()

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

    dataset = args.dataset
    learning_rate = args.learning_rate
    reg_rate = args.reg_rate
    margin = args.margin
    epoch = args.epochs
    batch_size = args.batch_size
    verbose = args.verbose
    t = args.t
    display_step = args.display_step
    num_factor = args.num_factor
    num_factor_mlp = args.num_factor_mlp
    hidden_dimension = args.hidden_dimension
    topK = args.topK
    lr_decay_rate = args.lr_decay_rate
    n = args.train_n
    test_n = args.test_n

    ranker_layers = [128, 64, 32, 8]

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    tf.reset_default_graph()
    print(
        "-----------------------------learning_rate: {}; num_factor_mlp: {}; reg_rate: {}---------------------------"
        .format(learning_rate, num_factor_mlp, reg_rate))
    print("!!!!!!!!!!!!!!!!Test_n: ", test_n, "!!!!!!!!!!!!!!!!!!!")
    with tf.Session(config=config) as sess:
        # sess = tf_debug.LocalCLIDebugWrapperSession(sess)
        # sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)
        model = None
        # Model selection
        if args.model == "Deep_CPL":
            num_users, train_num_items, train_u_input, train_i_input, train_j_input = get_pairwise_train_dataset(
                path='data/%d_top%d/%s_train.dat' % (n, topK, dataset))

            print("Number of users: {} and train_items: {}".
                format(num_users, train_num_items))
            # num_users, num_items = 943, 1683
            # num_users, num_items = 9649, 17770

            train_labels = [1 for _ in range(len(train_u_input))]
            print("Length of train_u_input: %d " % len(train_u_input))
            if ('jester' in dataset or 'lastfm' in dataset):
                test_num_items, testItems, testRatings = get_test_data(path='data/%d_top%d/%s_test_ratings.lsvm' % (n, topK, dataset))
            else:
                test_num_items, testItems, testRatings = get_test_data(path='data/%d_top%d/%s_test_ratings_%d.lsvm' % (n, topK, dataset, test_n))
            print("Number of test_items: ", test_num_items)
            num_items = max(train_num_items, test_num_items)

            num_users += 1
            num_items += 1
            # the number of batch of one epoch: the number of total positive pairs / batch_size
            total_batch = int(
                len(train_u_input) /
                batch_size)

            model = Deep_CPL(
                sess,
                dataset=dataset,
                num_user=num_users,
                num_item=num_items,
                learning_rate=learning_rate,
                reg_rate=reg_rate,
                margin=margin,
                epoch=epoch,
                batch_size=batch_size,
                verbose=verbose,
                t=t,
                display_step=display_step,
                num_factor=num_factor,
                num_factor_mlp=num_factor_mlp,
                hidden_dimension=hidden_dimension,
                topK=topK,
                lr_decay_rate=lr_decay_rate,
                ranker_layers=ranker_layers,
                test_n=test_n
                )

            model.prepare_data(train_u_input, train_i_input, train_j_input, train_labels, testItems, testRatings)

            model.build_network()

            model.execute()


import numpy as np
import torch
from torch.utils.data import TensorDataset,DataLoader,SubsetRandomSampler

class_path = 'class_dict.txt'

split_folder = ('standard_split/AWA2/')
split_version = 1
awa_folder = ('Animals_with_Attributes2/')

train_split_f = open(split_folder+"trainclasses"+str(split_version)+".txt")
val_split_f = open(split_folder+"valclasses"+str(split_version)+".txt")
features_f = open(awa_folder+"Features/ResNet101/AwA2-features.txt")
labels_f = open(awa_folder+"Features/ResNet101/AwA2-labels.txt")
class_f = open(awa_folder+"classes.txt")

def get_awa_data(use_val=True,class_split=False):
    train_size = 0.8
    batch_size = 256

    class_index = {}
    rev_class_index = {}
    for line in class_f:
        line = line.split("\t")
        class_index[line[1][:-1]] = int(line[0])
        rev_class_index[int(line[0])] = line[1][:-1]

    class_hash = {}
    train_classes = set([class_index[line[:-1]] if line != "" else -1 for line in train_split_f])
    val_classes = set([class_index[line[:-1]] if line != "" else -1 for line in val_split_f])
    n_train = len(list(train_classes))
    n_val = len(list(val_classes))
    class_dict = open(class_path,'w')
    for i,c in enumerate(list(train_classes)):
        class_hash[c] = i
        class_dict.write(str(i)+","+rev_class_index[c]+"\n")
    if use_val:
        for i,c in enumerate(list(val_classes)):
            class_hash[c] = i + n_train
            class_dict.write(str(i+n_train)+","+rev_class_index[c]+"\n")

    features = []
    labels = []
    train_features = []
    train_labels = []
    val_features = []
    val_labels = []
    for feature,label in zip(features_f,labels_f):
        label = int(label)
        if use_val and not class_split:
            if label in train_classes or label in val_classes:
                features.append(feature[:-1].split(" "))
                labels.append(class_hash[label])
        else:
            if label in train_classes:
                train_features.append(feature[:-1].split(" "))
                train_labels.append(class_hash[label])
            elif label in val_classes:
                val_features.append(feature[:-1].split(" "))
                val_labels.append(class_hash[label])
    if class_split:
        train_features = np.array(train_features,dtype=np.float32)
        train_labels = np.array(train_labels,dtype=np.float32)
        val_features = np.array(val_features,dtype=np.float32)
        val_labels = np.array(val_labels,dtype=np.float32)
        trainset = TensorDataset(torch.FloatTensor(train_features),torch.LongTensor(train_labels))
        trainloader = DataLoader(trainset,batch_size=batch_size,shuffle=False)
        valset = TensorDataset(torch.FloatTensor(val_features),torch.LongTensor(val_labels))
        valloader = DataLoader(valset,batch_size=batch_size,shuffle=False)
    else:
        if use_val:
            features = np.array(features,dtype=np.float32)
            labels = np.array(labels,dtype=np.int32)
        else:
            features = np.array(train_features,dtype=np.float32)
            labels = np.array(train_labels,dtype=np.int32)
        train_idx = []
        val_idx = []
        for i in range(len(features)):
            tr = np.random.binomial(1,train_size)
            if tr:
                train_idx.append(i)
            else:
                val_idx.append(i)
        dataset = TensorDataset(torch.FloatTensor(features),torch.LongTensor(labels))
        trainloader = DataLoader(dataset,batch_size=batch_size,sampler=SubsetRandomSampler(train_idx))
        valloader = DataLoader(dataset,batch_size=batch_size,sampler=SubsetRandomSampler(val_idx))
    return trainloader,valloader


import pickle as pk
import numpy as np
import networkx as nx
import scipy as sp
import torch
import infomap
import community
import math
import tensorflow as tf
import collections
import operator
from collections import defaultdict
from collections import OrderedDict
from itertools import permutations
from collections import Counter
from networkx.algorithms import tree

#ind.cora.allx  ind.cora.ally  ind.cora.graph  ind.cora.test.index  ind.cora.tx  ind.cora.ty  ind.cora.x  ind.cora.y

def get_test_index(f):
    f1 = open(f,"r")
    ids = []
    for _f in f1:
        _f = _f.strip("\n\r")
        ids.append(int(_f))
    _t = np.array(ids)
    #_t = np.sort(_t)
    return _t

def convert_adj_to_sparse(adj):
    sparse_adj = sp.sparse.coo_matrix(adj,dtype=np.float32)
    indices = np.vstack((sparse_adj.row,sparse_adj.col))
    values = sparse_adj.data
    i = torch.LongTensor(indices)
    v = torch.FloatTensor(values)
    s = sparse_adj.shape
    return torch.sparse.FloatTensor(i,v,torch.Size(s))

def get_data(cuda="True"):
    end = [".allx",".ally",".tx",".ty",".x",".y",".graph",".test.index"]
    _allx,_ally,_te_x,_te_y,_tr_x,_tr_y,_net,_test = [u[0]+u[1] for u in zip(["ind.cora"]*8,end)]  # Change the network here
    allx = pk.load(open(_allx,"rb"),encoding="latin1")
    ally = pk.load(open(_ally,"rb"),encoding="latin1")
    te_x = pk.load(open(_te_x,"rb"),encoding="latin1")
    te_y = pk.load(open(_te_y,"rb"),encoding="latin1")
    tr_x = pk.load(open(_tr_x,"rb"),encoding="latin1")
    tr_y = pk.load(open(_tr_y,"rb"),encoding="latin1")
    net = pk.load(open(_net,"rb"),encoding="latin1")
    test_idx = get_test_index(_test)
    test_idx_reorder = np.sort(test_idx)
    #print(tr_x.shape,te_x.shape,test_id.size,allx.shape)
    features = sp.sparse.vstack((allx,te_x)).tolil()  # because without lil format there is computational burden
    print(features.shape)
    features[test_idx,:] = features[test_idx_reorder,:]
    labels = np.vstack((ally,te_y))
    labels[test_idx,:] = labels[test_idx_reorder,:]
    idx_train = range(len(tr_y))
    idx_val = range(len(tr_y), len(tr_y)+500)
    g = nx.from_dict_of_lists(net)
    print("number of nodes,edges ",g.number_of_nodes(),g.number_of_edges())
    adj = nx.to_numpy_array(g,dtype=np.float)
    adj = adj + np.eye(adj.shape[0])   # A = A'
    #print(adj[5,2546],adj[2546,5])
    #adj = sp.sparse.coo_matrix(adj)
    adj = convert_adj_to_sparse(adj)
    adj = normalize_adj(adj)            # make this commented for GraphSage
    features = normalize_features(features)
    #adj = convert_adj_to_sparse(adj)  # this is converted to sparse format due to its immense size
    adj = torch.FloatTensor(adj.to_dense())
    features = torch.FloatTensor(np.array(features.todense())).float()
    labels = torch.LongTensor(labels)
    labels = torch.max(labels, dim=1)[1]
    #print(labels)
    idx_train = torch.LongTensor(idx_train)
    idx_val = torch.LongTensor(idx_val)
    idx_test = torch.LongTensor(test_idx_reorder)
    if cuda:
        features = features.cuda()
        adj = adj.cuda()
        labels = labels.cuda()
        idx_train = idx_train.cuda()
        idx_val = idx_val.cuda()
        idx_test = idx_test.cuda()

    #return g,adj,features,labels,idx_train,idx_val,idx_test
    return idx_train,idx_test,idx_val

def get_data_v1(cuda=True):

# Here the data is obtained from pytorch-geometric to eliminate unnecessary shuffling done in Kipf's code
    edge_index = pk.load(open("graph.pkl","rb"))
    row,col = edge_index
    edges = [(int(u),int(v)) for u,v in zip(row.tolist(),col.tolist())]
    g = nx.Graph()
    g.add_edges_from(edges)
    adj = np.zeros((torch.max(edge_index).item()+1,torch.max(edge_index).item()+1))
    for u,v in list(g.edges()):
        adj[u,v] = 1
        adj[v,u] = 1
    adj = nx.to_numpy_array(g,dtype=np.float)
    adj = adj + np.eye(adj.shape[0])
    adj = sp.sparse.coo_matrix(adj)
    #adj = normalize_adj(adj)
    adj = torch.FloatTensor(adj.todense())
    features = pk.load(open("feature.pkl","rb"))
    features = normalize_features(features.numpy())
    features = torch.FloatTensor(features)
    labels = pk.load(open("label.pkl","rb"))
    idx_train = pk.load(open("train_ids.pkl","rb"))
    idx_val = pk.load(open("valid_ids.pkl","rb"))
    idx_test = pk.load(open("test_ids.pkl","rb"))
    print(len(idx_train),len(idx_test),len(idx_val))
    if cuda:
        features = features.cuda()
        adj = adj.cuda()
        labels = labels.cuda()
        #idx_train = idx_train.cuda()
        #idx_val = idx_val.cuda()
        #idx_test = idx_test.cuda()
    #return g,adj,features,labels,idx_train,idx_val,idx_test
    return idx_train,idx_test,idx_val

'''def get_data_v2(cuda=True):

# Here the data is obtained from pytorch-geometric to eliminate unnecessary shuffling done in Kipf's code
    edge_index = pk.load(open("graph.pkl","rb"))
    row,col = edge_index
    edges = [(int(u),int(v)) for u,v in zip(row.tolist(),col.tolist())]
    g = nx.Graph()
    g.add_edges_from(edges)
    print("Graph Read ")
    nodes = nx.nodes(g)
    print ("nodes:",nodes)
    adj = np.zeros((torch.max(edge_index).item()+1,torch.max(edge_index).item()+1))
    for u,v in list(g.edges()):
        adj[u,v] = 1
        adj[v,u] = 1
    adj = nx.to_numpy_array(g,dtype=np.float)
    adj = adj + np.eye(adj.shape[0])
    adj = sp.sparse.coo_matrix(adj)
    print("Adjacency Made")
    adj = torch.FloatTensor(adj.todense())
    features = pk.load(open("feature.pkl","rb"))
    features = normalize_features(features.numpy())
    features = torch.FloatTensor(features)
    print("Features Normalized ")
    labels = pk.load(open("label.pkl","rb"))
    #idx_train = pk.load(open("train_ids.pkl","rb"))
    #idx_val = pk.load(open("valid_ids.pkl","rb"))
    #idx_test = pk.load(open("test_ids.pkl","rb"))
    info = infomap.Infomap("--two-level --silent")
    for e in list(g.edges()):
        info.addLink(*e)
    info.run()
    c = info.getModules()
    z = defaultdict(list)
    for u in c:
        z[c[u]].append(u)
    c1 = list(z.values())
    print ("communities",len(c1))
    c1_arr = np.array(c1)
    #temp_data = np.array([nx.density(g.subgraph(z)) for z in c1],dtype=np.float)
    temp_data = np.array([len(z) for z in c1],dtype=np.float)
    temp_data_idx = np.argsort(temp_data)
    c1_arr = c1_arr[temp_data_idx][::-1] # descending
    total = int(0.1 * g.number_of_nodes())
    train_ids = []
    val_ids = []
    test_ids = []
    for z in c1_arr:
        g1 = g.subgraph(z)
        total_node = list(g1.nodes())
        g2 = nx.k_core(g1)
        n1 = list(g2.nodes())
        tar_nodes = np.random.choice(n1,int(0.5*len(n1)),replace=False)
        other_nodes = list(set(total_node).difference(set(tar_nodes)))
        tar_nodes1 = list(set(n1).difference(set(tar_nodes)))
        if len(train_ids) <= total:
            train_ids.extend(tar_nodes)
        test_ids.extend(other_nodes)
        val_ids.extend(tar_nodes1)

    idx_train = np.array(train_ids)
    idx_val = np.array(val_ids)
    idx_test = np.array(test_ids)
    print("Train Validation Test ",len(idx_train),len(idx_val),len(idx_test))
    print("No of Communities {}".format(len(c1)))

    if cuda:
        features = features.cuda()
        adj = adj.cuda()
        labels = labels.cuda()
        #idx_train = idx_train.cuda()
        #idx_val = idx_val.cuda()
        #idx_test = idx_test.cuda()
    #return g,adj,features,labels,idx_train,idx_val,idx_test
    return idx_train,idx_test,idx_val'''

def bfs(graph, root):
    visited, queue = set(), collections.deque([root])
    visited.add(root)
    while queue:
        vertex = queue.popleft()
        for neighbour in graph[vertex]:
            if neighbour not in visited:
                visited.add(neighbour)
                queue.append(neighbour)
    return visited

def get_data_v3(cuda=True):

# Here the data is obtained from pytorch-geometric to eliminate unnecessary shuffling done in Kipf's code
    edge_index = pk.load(open("graph.pkl","rb"))
    row,col = edge_index
    edges = [(int(u),int(v)) for u,v in zip(row.tolist(),col.tolist())]
    g = nx.Graph()
    g.add_edges_from(edges)
    print("Graph Read ")

    nnodes = nx.number_of_nodes(g)
    nodes = nx.nodes(g)
    #print(nodes)
    cr = dict(nx.core_number(g))
    cr_vals = set(v for v in cr.values())
    cr_dict = {}
    for d in cr_vals:
        tmp = []
        for k,v in cr.items():
            if v == d:
                tmp.append(k)
        cr_dict[d] = tmp
    print ("core numbers of original graph", len(cr_vals))

    print("number of nodes--",nnodes)
    cut = int(0.1*nnodes)
    print ("cut value--",cut)
    #print("number of nodes,edges ",g.number_of_nodes(),g.number_of_edges())
    adj = np.zeros((torch.max(edge_index).item()+1,torch.max(edge_index).item()+1))
    for u,v in list(g.edges()):
        adj[u,v] = 1
        adj[v,u] = 1
    adj = nx.to_numpy_array(g,dtype=np.float)
    adj = adj + np.eye(adj.shape[0])
    adj = sp.sparse.coo_matrix(adj)
    print("Adjacency Made")

    adj = torch.FloatTensor(adj.todense())
    features = pk.load(open("feature.pkl","rb"))
    features = normalize_features(features.numpy())
    features = torch.FloatTensor(features)
    print("Features Normalized ")

    labels = pk.load(open("label.pkl","rb"))
    lb = labels.numpy()
    ground_dict = Counter(lb)
    classes = len(ground_dict)

    #community detection --Infomap
    info = infomap.Infomap("--two-level --silent -s 8")
    for e in list(g.edges()):
        info.addLink(*e)
    info.run()
    c = info.getModules() #node:community
    z = defaultdict(list)
    for u in c:
        z[c[u]].append(u) #community:[nodes]
    #print("number of communities detected")
    #print (len(z))
    com_size = {}
    for k,v in z.items():
        com_size[k] = len(v)
    #print(com_size)

    #community detection-- Louvain
    partition = community.best_partition(g) #node:community
    com = defaultdict(list)
    for p in partition:
            com[partition[p]].append(p)
    print("number of communities detected")
    print (len(com))

    a = set()
    a_wt = []
    for te in edges:
        u = te[0]
        v = te[1]
        com_u = partition[u]
        com_v = partition[v]
        t = (com_u,com_v)
        a.add(t)
        if com_u > com_v :
            m = (com_v,com_u)
            a_wt.append(m)
        else:
            a_wt.append(t)

    edge_wt = Counter(a_wt)
    #print(edge_wt)

    meta_wt_edge = {}
    #print(len(a))
    meta_nodes = list(com.keys())
    #print (len(meta_nodes))
    per = list(permutations(meta_nodes,2))
    b = set()
    for cc in per:
        b.add(cc)
    meta_edge = a.intersection(b)

    for k,v in edge_wt.items():
        if k in meta_edge:
            meta_wt_edge[k] = v

    #print("meta edges")
    #print(meta_wt_edge)

    meta_net =  nx.Graph()
    meta_net.add_nodes_from(meta_nodes)
    meta_net.add_edges_from(meta_edge)
    print ("meta graph formed")

    m_nodes = nx.number_of_nodes(meta_net)
    print ("number of meta nodes",m_nodes)

    m_edges = meta_net.number_of_edges()
    print ("number of meta edges",m_edges)

    train_ids = []

    edge_set = set(edges)
    for m in meta_nodes:
        coms = com[m]
        perm = set(permutations(coms,2))
        in_edges = edge_set.intersection(perm)
        #print(in_edges)
        in_net = nx.Graph()
        in_net.add_edges_from(in_edges)
        #print(in_net.edges())
        in_clus = nx.clustering(in_net)
        #print("clustering",in_clus)
        h = max(in_clus.items(), key = operator.itemgetter(1))[0]
        train_ids.append(h)

    #meta_edgelist = list(meta_net.edges())

    '''cores = dict(nx.core_number(meta_net))

    mst = nx.minimum_spanning_tree(meta_net, algorithm='prim')
    #print("tree edges",mst.edges())
    mst_edgelist = list(sorted(mst.edges()))
    mst_nodes =  list(mst.nodes())
    mst_adj = {}
    for s in mst_nodes:
        mst_l = []
        for e in mst_edgelist:
            if s == e[0] :
                mst_l.append(e[1])
        mst_adj[s] = mst_l

    #print(mst_adj)
    #print(mst_edgelist)
    core_vals = set(v for v in cores.values())
    core_dict = {}
    for d in core_vals:
        tmp = []
        for k,v in cores.items():
            if v == d:
                tmp.append(k)
        core_dict[d] = tmp'''

    #print(core_dict)
    #print ("number of cores in meta network:", len(core_dict))

    '''core_class = {}
    for k,v in core_dict.items():
        cls = []
        for m in v:
            nd = z[m]
            for x in nd:
                cl = lb[x]
                cls.append(cl)
        core_lb = Counter(cls)
        mm =  max(v for k,v in core_lb.items())
        for k1,v1 in core_lb.items():
            if v1 == mm:
                core_class[k]=k1
    print("class information per core--")
    print(core_class)   #The class information/core is printed

    com_class = {}
    for mn in meta_nodes:
        cls = []
        nd = z[mn]
        for x in nd:
            cl = lb[x]
            cls.append(cl)
        com_lb = Counter(cls)
        mm = max(v for k,v in com_lb.items())
        for k1,v1 in com_lb.items():
            if v1 == mm :
                com_class[mn] = k1

    print("class information per community--")
    #print(com_class) #The class information/community is printed

    com_cls = []
    for k,v in com_class.items():
        com_cls.append(v)
    print(Counter(com_cls))

    sorted_core = dict(OrderedDict(sorted(core_dict.items(),reverse=True)))

    reverse_core = dict(OrderedDict(sorted(sorted_core.items())))'''

    '''t_n = []
    for v in sorted_core[25]:
        for t in z[v]:
            t_n.append(t)
    t_lb = []
    for t in t_n:
        t_lb.append(lb[t])''' #for checking the class labels distribution in each core

    #build 2nd order network--

    '''meta_info = infomap.Infomap("--two-level --silent -s 8")
    for e in list(meta_net.edges()):
        meta_info.addLink(*e)
    meta_info.run()
    cc = meta_info.getModules() #node:community
    zz = defaultdict(list)
    for u in cc:
        zz[cc[u]].append(u) #community:[nodes]
    print("number of meta communities detected")
    print (len(zz))

    meta_coms = {}
    for k,v in zz.items():
        cls = []
        for b in v:
            lbl = com_class[b]
            cls.append(lbl)
        metacom_lb = Counter(cls)
        meta_coms[k] = metacom_lb

    print("class information of meta communities of 2nd order network--")
    print(meta_coms)

    meta_cr = dict(nx.core_number(meta_net))
    meta_cr_vals = set(v for v in meta_cr.values())
    meta_cr_dict = {}
    for d in meta_cr_vals:
        tmp = []
        for k,v in meta_cr.items():
            if v == d:
                tmp.append(k)
        meta_cr_dict[d] = tmp
    print("cores in 2nd order network--")
    print(meta_cr_dict)

    #Selection of training nodes
    core_window =3

    t_cores = []
    cnt = 0
    for cr,coms in sorted_core.items():
        t_cores.append(cr)
        cnt += 1
        if cnt == core_window:
            break
    print("t_cores--",t_cores)
    #print("t_coms--",len(t_coms))

    #build adjacency matrix of edges--
    t_coms = core_dict[7]
    p = len(t_coms)
    rows,cols = (p,p)
    adje = [[0]*cols]*rows
    for me in meta_edgelist:
        u = me[0]
        v = me[1]
        if u in t_coms:
            if v in t_coms:
                #h += 1
                ui = t_coms.index(u)
                vi = t_coms.index(v)
                adje[ui][vi] += 1
    #print(adje)'''
    '''for me in meta_edge:
        u = me[0]
        if u == 5:
            print(me)'''

    '''t_arr = []
    for i in range(core_window):
        t_arr.append(0)

    tr_dict = {}
    for cls in range(classes):
        tr_nodes = []
        fl  = 0
        ar = 0
        cnt_cls = int(0.1*(ground_dict[cls]))
        print("cls and count--",cls,cnt_cls)
        while(True):
            for cr in t_cores:
                coms = core_dict[cr]
                j = t_arr[ar]
                cm = coms[j]
                j = (j+1)%len(coms)
                t_arr[ar] = j
                ar += 1
                #cm = int(np.random.choice(coms,1))
                nn = z[cm]
                n = int(np.random.choice(nodes,1))
                l = lb[n]
                if l == cls and n not in tr_nodes:
                    tr_nodes.append(n)
                    if len(tr_nodes) == cnt_cls:
                        fl = 1
                        break
                if ar == core_window:
                    ar = 0
            if fl == 1:
                tr_dict[cls] = tr_nodes
                break

    t_lbls = []
    for k,v in tr_dict.items():
        for t in v:
            lbl = lb[t]
            t_lbls.append(lbl)

    print("class level distribution--training labels",Counter(t_lbls))

    train_ids = []
    val_ids = []
    test_ids = []
    test_mask_ids = []
    for k,v in tr_dict.items():
        for t in v:
            train_ids.append(t)
    #for n in nodes2:
        #train_ids.append(n)
    f = 0
    while True:
        if len(train_ids)<cut:
            r = int(np.random.choice(nodes,1,replace = False))
            if r not in train_ids:
                train_ids.append(r)
                if len(train_ids)==cut:
                    f = 1
        if f == 1:
            break
    #print("train ids--",len(train_ids))'''
    #sorted_core = dict(OrderedDict(sorted(core_dict.items(),reverse=True)))
    #print(sorted_core)

    #c_meta_nodes = sorted_core[7]
    #y = int(np.random.choice(c_meta_nodes,1))

    #train_ids = []
    #train_coms = bfs(mst_adj,y)
    #print(train_coms)

    '''f = 0
    while True:
        for tc in train_coms:
            yy = z[tc]
            x = int(np.random.choice(yy,1))
            train_ids.append(x)
            if len(train_ids) == cut :
                f = 1
                break
        if f == 1:
            break
        else:
            continue'''

    #print(train_ids)

    #train-test nodes choice

    '''for m in meta_nodes:
        f_nodes = z[m]
        x = int(np.random.choice(f_nodes,1,replace=False))
        train_ids.append(x)'''

    val_ids = []
    test_ids = []
    rm_ids = []

    for n in nodes:
        if n not in train_ids:
            #if n not in nodes2:
                rm_ids.append(n)
    #print ("test ids--",len(test_ids))

    #val_ids.extend(rm_ids[0:int(0.1*len(nodes))])
    val_ids = np.random.choice(rm_ids,len(train_ids),replace=False)

    r_ids = []
    for n in rm_ids:
        if n not in val_ids:
            r_ids.append(n)
    #val_ids= np.random.choice(test_ids,int(0.1*len(nodes)),replace= False)
    test_ids = np.random.choice(r_ids,1084,replace = False)
    #val_ids = np.random.choice(test_ids,int(0.1*len(nodes)),replace= False)
    #test_mask_ids = np.random.choice(test_ids,1084,replace = False)

    with open("test_labels_infomap.txt",'wb') as fp:
        pk.dump(test_ids,fp)

    with open("training_labels_infomap.txt","wb") as fp:
        pk.dump(train_ids,fp)

    idx_train = np.array(train_ids)
    idx_val = np.array(val_ids)
    idx_test = np.array(test_ids)
    print("Train Validation Test ",len(idx_train),len(idx_val),len(idx_test))

    if cuda:
        features = features.cuda()
        adj = adj.cuda()
        labels = labels.cuda()
        #idx_train = idx_train.cuda()
        #idx_val = idx_val.cuda()
        #idx_test = idx_test.cuda()
    #return g,adj,features,labels,idx_train,idx_val,idx_test
    return idx_train,idx_test,idx_val

def normalize_adj(m):
    print("In adj Nomalizee")
    rowsum = np.array(m.sum(1))
    rowsum = np.power(rowsum,-1).flatten()
    rowsum[np.isinf(rowsum)] = 0  # error handling
    D = sp.sparse.diags(rowsum)
    adj = D.dot(m).dot(D)     # D^-1*A'D^-1
    #print(adj)
    #print(type(adj))
    #adj = np.array(adj,dtype=np.float32)
    return adj

def normalize_features(m):
    rowsum = np.array(m.sum(1))
    z_idx = np.where(rowsum==0)[0]
    rowsum[z_idx] = 1                    # for cases where all features are zeros
    rowsum = np.power(rowsum,-1).flatten()
    rowsum[np.isinf(rowsum)] = 0  # error handling
    D = sp.sparse.diags(rowsum)
    F = D.dot(m)     # D^-1*F
    return F

def accuracy(output, labels):
    preds = output.max(1)[1].type_as(labels)
    correct = preds.eq(labels).double()
    correct = correct.sum()
    return correct / len(labels)


"""
@file    :  data.py
@License :  (C)Copyright 2021 Haoran Jia, Fudan University. All Rights Reserved
@Contact :  21211140001@m.fudan.edu.cn
@Desc    :

@Modify Time      @Author      @Version
------------      -------      --------
                  JHR          1.0
"""
import tensorflow as tf
import os
import matplotlib.pyplot as plt
from utils import DataPreprocess

"""
    输入图像预处理函数
"""

def load_jpg(fdir):
    """
    读取单张图片，大小(420, 360, n)，格式uint8
    :param fdir: 图片的地址
    :return: ndarray
    """
    image = tf.io.read_file(fdir)
    image = tf.io.decode_jpeg(image)
    image = tf.cast(image, dtype=tf.float32)
    return image

def normalize(image):
    image = (image / 255)
    return image

def resize(image):
    """
    在420*360的图片周围填充0，到512*512
    :param image:
    :return:
    """
    image = tf.image.resize_with_crop_or_pad(image, 512, 512)
    return image

"""
    dataset最终加载函数，用在Dataset_instance.map()中
    输入：单个图像的路径
"""

def load_image(fdir):
    """
    :param fdir: 单个输入图像路径
    :return: 最终用于训练的单个图像数据
    """
    image = load_jpg(fdir)
    image = resize(image)
    image = normalize(image)
    return image

"""
    生成Dataset
"""

def create_dataset(split_method, origin_path, seg_path, batch_size=1):
    # 包含分割方法的文件夹
    split_path = os.path.join("dataset\\split", split_method)

    # 生成数据集，其中每个元素都是数据的完整路径
    # 超声影像 origin_path
    train_origin = DataPreprocess.add_folder_path(origin_path, os.path.join(split_path, "train.txt"))
    train_dataset_origin = tf.data.Dataset.from_tensor_slices(train_origin)
    validation_origin = DataPreprocess.add_folder_path(origin_path, os.path.join(split_path, "validation.txt"))
    validation_dataset_origin = tf.data.Dataset.from_tensor_slices(validation_origin)
    test_origin = DataPreprocess.add_folder_path(origin_path, os.path.join(split_path, "test.txt"))
    test_dataset_origin = tf.data.Dataset.from_tensor_slices(test_origin)

    # 分割数据 seg_path
    train_seg = DataPreprocess.add_folder_path(seg_path, os.path.join(split_path, "train.txt"))
    train_dataset_seg = tf.data.Dataset.from_tensor_slices(train_seg)
    validation_seg = DataPreprocess.add_folder_path(seg_path, os.path.join(split_path, "validation.txt"))
    validation_dataset_seg = tf.data.Dataset.from_tensor_slices(validation_seg)
    test_seg = DataPreprocess.add_folder_path(seg_path, os.path.join(split_path, "test.txt"))
    test_dataset_seg = tf.data.Dataset.from_tensor_slices(test_seg)

    # 通过文件路径数据集读取、处理图片数据，将超声和分割结合
    # 训练集
    train_dataset_origin = train_dataset_origin.map(load_image)
    train_dataset_seg = train_dataset_seg.map(load_image)
    train_dataset = tf.data.Dataset.zip((train_dataset_origin, train_dataset_seg))
    # 验证集
    if validation_origin:
        validation_dataset_origin = validation_dataset_origin.map(load_image)
        validation_dataset_seg = validation_dataset_seg.map(load_image)
    validation_dataset = tf.data.Dataset.zip((validation_dataset_origin, validation_dataset_seg))
    # 测试集
    test_dataset_origin = test_dataset_origin.map(load_image)
    test_dataset_seg = test_dataset_seg.map(load_image)
    test_dataset = tf.data.Dataset.zip((test_dataset_origin, test_dataset_seg))

    # 对数据集进行后续处理
    train_dataset = train_dataset.shuffle(len(train_origin)).batch(batch_size)
    validation_dataset = validation_dataset.batch(1)
    test_dataset = test_dataset.batch(1)

    return train_dataset, validation_dataset, test_dataset


from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import SkillSerializer
from packages.globalfunction import *

from rest_framework import status, generics, permissions
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope, TokenHasScope

# ------------------------------------------------------ get skills list -------------------------------------------------
class SkillList(APIView):
	'''
		Parameters
			user_id : required \n
	'''
	permission_classes = [permissions.IsAuthenticated, TokenHasReadWriteScope]
	def post(self,request,*args, **kwargs):

		errorDict = isUserExist(request)
		if errorDict:
			return Response(responsejson(errorDict,status=422))

		postData = managePostData(request.POST)

		try:
			# skills = Skill.objects.all().only('id','name').order_by('-name')
			skills = Skill.objects.filter(name__icontains=postData['skill_starts']).only('id','name').order_by('name')
			skillDict = []
			for skill in skills:
				singleSkill = SkillSerializer(skill)
				singleSkill = makeBlank(singleSkill.data.items())
				skillDict.append(singleSkill)

			return Response(responsejson(skillDict,status=200))
		except Exception as e:
			print(e)
			errorDict['error'] = "There are no skills"
			return Response(responsejson(errorDict,status=422))


# Simple application to scan directories.

import os

def search():
    print("DirectoryScanner v1.0.0")
    _dir = input("Which directory should I search?\n > ")
    _ext = input("Which file extension should I look for?\n > ")
    try:
        _dir
    except NameError:
        print("Error: Directory not defined.")
        sys.exit()
    else:
        print("Finding results...")
        c = 0
        ct = 0
        if os.path.isdir(_dir):
            for file in os.listdir(_dir):
                name = os.path.splitext(os.path.join(_dir, file))[0]
                if _ext == '':
                    print(" " + os.path.join(_dir, file))
                else:
                    if file.endswith(_ext):
                        print(" " + os.path.join(_dir, file))
                    else:
                        ct = ct + 1
            if c < ct:
                print("Error: No files matching that search criteria.")
            continue_search()
        else:
            print("Error: Unknown directory.")
            continue_search()

def continue_search():
    _b = input("Search another directory?\n > ")
    if _b.upper() == "TRUE" or _b.upper() == "YES" or _b.upper() == "Y":
        search()
    else:
        print("Thanks for using DirectoryScanner.")

search()


from os.path import dirname, join
from image_sample import SampleWithCache
import multiprocessing
from functools import partial
from utils import list_images

def _parse_line(line, imgs_dir, label_size, max_scale):
    parts = line.split(';')
    local_path = parts[0]
    if label_size > 0:
        annotation = parts[1:]
    else:
        annotation = None
    img_path = join(imgs_dir, local_path)
    image_sample = SampleWithCache(img_path, annotation=annotation, max_scale=max_scale)
    return image_sample

def load_samples(input_dir, label_size=0, samples_file=None, workers=0, max_scale=64):
    if samples_file is not None:
        csv_path = join(input_dir, samples_file)
        samples = load_images_from_csv(csv_path, label_size, workers, max_scale)
    else:
        assert label_size == 0
        samples = load_images_from_dir(input_dir, workers, max_scale)
    return samples

def load_images_from_dir(imgs_dir, workers, max_scale):

    imnames = list_images(imgs_dir)

    if workers == 0:
        samples = [_parse_line(imname, imgs_dir, 0, max_scale) for imname in imnames]
    else:
        pool = multiprocessing.Pool(processes=workers)
        samples = pool.map(partial(_parse_line, imgs_dir=imgs_dir, label_size=0, max_scale=max_scale), imnames)
        pool.close()
        pool.join()
        assert(len(samples) == len(imnames))

    return samples

def load_images_from_csv(csv_path, label_size, workers, max_scale):
    imgs_dir = dirname(csv_path)
    with open(csv_path) as fp:
        content = fp.read().splitlines()

    if workers == 0:
        samples = [_parse_line(line, imgs_dir, label_size, max_scale) for line in content]
    else:
        pool = multiprocessing.Pool(processes=workers)
        samples = pool.map(partial(_parse_line, imgs_dir=imgs_dir, label_size=label_size, max_scale=max_scale), content)
        pool.close()
        pool.join()
        assert(len(samples) == len(content))

    return samples


import json
#import xml.etree.ElementTree as ET
from lxml import etree as ET

papers_file = open('papers.json','r')
papers_content = papers_file.read()[len("entities="):]
papers = json.loads(papers_content)

sessions_file = open('sessions.json','r')
sessions_content = sessions_file.read()[len("sessions="):]
sessions = json.loads(sessions_content)

schedule_file = open('schedule.json','r')
schedule_content = schedule_file.read()[len("schedule="):]
schedule = json.loads(schedule_content)

def update_session_id():

	ns = '{http://ns.adobe.com/AdobeInDesign/5.0/}'
	blues = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Session_Blue']".format(ns))
	greens = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Session_Green']".format(ns))
	reds = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Session_Red']".format(ns))
	golds = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Session_Gold']".format(ns))

	print(len(blues))
	print(len(greens))
	print(len(reds))
	print(len(golds))
	#print()

	for element in blues+greens+reds+golds:
		element.set('class','parallel_session')

	elements = root.findall("./AtAGlance/Day_Table/Table/Cell[@class='parallel_session']/Session_Title")
	#print(elements)
	for element in elements:
		element.text= ''#'TITLE'
	elements = root.findall("./AtAGlance/Day_Table/Table/Cell[@class='parallel_session']/Session_Type")
	#print(elements)
	for element in elements:
		element.text='' #'TYPE'

	elements = root.findall(".//Session_Code")
	#print(elements)
	for element in elements:
		element.getparent().remove(element)

	plenaries = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Plenary']".format(ns))
	#print(len(plenaries))
	plenaries[0].set("sessionid", "day0_slot3_room17")
	plenaries[2].set("sessionid", "day0_slot3_room17")
	plenaries[1].set("sessionid", "day1_slot1_room17")
	plenaries[3].set("sessionid", "day1_slot1_room17")

	plenaries[4].set("sessionid", "day2_slot1_room17")
	plenaries[7].set("sessionid", "day2_slot1_room17")

	plenaries[5].set("sessionid", "day3_slot1_room17")
	plenaries[8].set("sessionid", "day3_slot1_room17")

	plenaries[6].set("sessionid", "day3_slot9_room17")
	plenaries[9].set("sessionid", "day3_slot9_room17")

	plenaries_title = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Plenary']/Session_Title".format(ns))
	for t in plenaries_title:
		t.text='TITLE'

	breaks = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Break']".format(ns))

	#print(len(plenaries))
	#for numpage in range(0,5):
	#	for numday in range (0,2):
	#		for numbreak in range(0, 6)
	#			breaks[numpage*numday*6+numbreak].set("sessionid", "day%s_slot%s"%(,numbreak*2+1))

	for numbreak in range(0,5):
		breaks[numbreak].set("sessionid", "day0_slot%s"%(numbreak*2))
	for numbreak in range(0,6):
		breaks[5+numbreak].set("sessionid", "day1_slot%s"%(numbreak*2))

	for numbreak in range(0,5):
		breaks[11+numbreak].set("sessionid", "day0_slot%s"%(numbreak*2))
	for numbreak in range(0,6):
		breaks[16+numbreak].set("sessionid", "day1_slot%s"%(numbreak*2))

	for numbreak in range(0,6):
		breaks[22+numbreak].set("sessionid", "day2_slot%s"%(numbreak*2))
	for numbreak in range(0,5):
		breaks[28+numbreak].set("sessionid", "day3_slot%s"%(numbreak*2))

	for numbreak in range(0,6):
		breaks[33+numbreak].set("sessionid", "day2_slot%s"%(numbreak*2))
	for numbreak in range(0,5):
		breaks[39+numbreak].set("sessionid", "day3_slot%s"%(numbreak*2))

	breaks_title = root.findall("./AtAGlance/Day_Table/Table/Cell[@{0}cellstyle='Break']/Break_Title".format(ns))
	#print(len(breaks_title))
	for t in breaks_title:
		t.text = ''

	tree.write('ataglance-01-class.xml')

	param_name='sessionid'

	day=0
	#slot=2
	#room=0

	for (listelem, w, start_room) in [ (blues, 4, 0), (greens,4, 4), (reds, 5, 8), (golds, 3, 13)]:
		slot = 5
		room=start_room
		start=0
		for i in range(0,3):
			for index in range(start+i*w, start+(i+1)*w):
				e = listelem[index]
				e.set(param_name,'day%s_slot%s_room%s'%(day,slot,room))
				#print('day%s_slot%s_room%s'%(day,slot,room))
				room+=1
			slot += 2
			room=start_room

	day=1

	for (listelem, w, start_room) in [ (blues, 4, 0), (greens,4, 4), (reds, 5, 8), (golds, 3, 13)]:
		slot = 3
		room=start_room
		start=w*3
		for i in range(0,4):
			for index in range(start+i*w, start+(i+1)*w):
				e = listelem[index]
				e.set(param_name,'day%s_slot%s_room%s'%(day,slot,room))
				#print('day%s_slot%s_room%s'%(day,slot,room))
				room+=1
			slot += 2
			room=start_room

	day=2

	for (listelem, w, start_room) in [ (blues, 4, 0), (greens,4, 4), (reds, 5, 8), (golds, 3, 13)]:
		slot = 3
		room=start_room
		start=w*7

		for i in range(0,4):
			for index in range(start+i*w, start+(i+1)*w):
				e = listelem[index]
				e.set(param_name,'day%s_slot%s_room%s'%(day,slot,room))
				#print('day%s_slot%s_room%s'%(day,slot,room))
				room+=1
			slot += 2
			room=start_room

	day=3

	for (listelem, oldw, w, start_room) in [ (blues, 4, 5, 0), (greens, 4, 3, 5), (reds, 5, 5, 8), (golds, 3, 3, 13)]:
		#print(start_room)
		slot = 3
		room=start_room
		start=oldw*11
		#print(start)
		for i in range(0,3):
			for index in range(start+i*w, start+(i+1)*w):
				#print(index, len(listelem))
				e = listelem[index]
				e.set(param_name,'day%s_slot%s_room%s'%(day,slot,room))
				#print('day%s_slot%s_room%s'%(day,slot,room))
				room+=1
			slot += 2
			room=start_room

	tree.write('ataglance-02-sessionid.xml')

#tree = ET.parse('ataglance-00.xml')
tree = ET.parse('ataglance-15.xml')
root = tree.getroot()

output=''

max_session=0

from collections import defaultdict
allrooms = defaultdict(int)
allslots = defaultdict(int)
alldays = defaultdict(int)
alltypes = defaultdict(int)

# indexes= {
# 'Monday':0,
# 'Tuesday':1,
# 'Wednesday':2,
# 'Thursday':3,
# '7:00 - 8:20':0,
# '7:30 - 8:30':0,
# '8:30 - 8:50':1,
# '8:50 - 9:00':2,
# '9:00 - 10:20':3,
# '10:20 - 11:00':4,
# '11:00 - 12:20':5,
# '12:20 - 14:00':6,
# '14:00 - 15:20':7,
# '15:20 - 16:00':8,
# '16:00 - 17:20':9,
# '17:30 - 19:00':10,
# }

#rooms = ['701A', '701B', '709', '711', '713AB', '714AB', '715A', '716A', '716B', '717AB', '718A', '718B', '801A', '801B', '802AB', '801AB', '715B', 'Exhibit Hall G']

#i=0
#for r in rooms:
#	indexes[r]=i
#	i+=1

# type_actual_str = {
# 'keynote': 	'Keynote',
# 'SIG': 		'SIG',
# 'panel': 	'Panel',
# 'altchi': 	'alt.chi',
# 'course': 	'Course',
# 'paper': 	'Papers',
# 'casestudy': 'Case studies',
# 'special': 	'Special',
# 'plenary':	'Plenary'
# }

#print(indexes)

import config

update_session_id()

elements = root.findall("./AtAGlance/Day_Table/Table/Cell/Room_Name")

# numpage=0
# numcell=0
# for e in elements:
# 	e.text='ROOM'
# 	e.set('rommid', numcell)
# 	numcell += 1
# 	if numcell = 16: numcell=0

for r in config.rooms:
	i=config.indexes[r]
	elementss = root.findall("./AtAGlance/Day_Table/Table/Cell[@roomid='%s']/Room_Name"%i)
	for e in elementss:
		e.text = r
		#print(e.getparent().get('roomid'))
#print(len(elements))

from itertools import cycle
def zip_cycle(A,B):
	return zip(A, cycle(B)) if len(A) > len(B) else zip(cycle(A), B)

# for element, room in zip_cycle(elements[0:16],rooms[0:8]):
# 	print(room)
# 	element.text=room
# print()
# for element, room in zip_cycle(elements[16:32],rooms[8:16]):
# 	print(room)
# 	element.text=room
# print()
#
# for element, room in zip_cycle(elements[32:48],rooms[0:8]):
# 	print(room)
# 	element.text=room
# print()
# for element, room in zip_cycle(elements[48:64],rooms[8:16]):
# 	print(room)
# 	element.text=room
# print()

shorten_session_names_out = ""
shorten_session_names_in = {}

shorten_dump = not True
shorten_grab= not shorten_dump

if shorten_grab:
	lines = open("shorten_session_names.csv", 'r').readlines()
	for line in lines:
		full, shorten = line[:-1].split('\t')
		#print (full,shorten)
		shorten_session_names_in[full]=shorten

for day in schedule:
	day_str=day['day']

	alldays[day_str] += 1
	output += day_str + '\n'

	for slot in day['slots']:
		slot_str=slot['time']

		output+="\t"+slot_str+'\n'
		allslots[slot_str]+=1

		session_index=0
		for session_info in slot['sessions']:
			#print(session)
			session_index+=1
			if session_index>max_session: max_session=session_index

			sessionid = session_info['session']
			try:
				session = sessions[sessionid]
			except KeyError as e:
				print('***********'+str(e))
			else:
				title_str = session['s_title']
				room_str = session['room']
				type_str = session['type']

				#print ("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+room_str)

				output+="\t\t"+title_str+' '+room_str+' '+type_str+'\n'
				allrooms[room_str]+=1
				alltypes[type_str]+=1

				if shorten_dump:
					shorten_session_names_out+=title_str+'\t'+title_str+'\n'

				if shorten_grab:
					try:
						title_str=shorten_session_names_in[title_str]
					except KeyError as e:
						print ('**** shorten:  '+str(e))
						pass

				#if type_str=='special':
				if config.indexes[slot_str]%2==0:
					text = ' • '+title_str
					if room_str:
						text += ' (' + room_str + ')'
					print ("**break title", day_str, slot_str, text)
					if False: # break titles are not all in the json...
					# xpath = "./AtAGlance/Day_Table/Table/Cell[@sessionid='day%s_slot%s']"
						xpath = xpath%(config.indexes[day_str],config.indexes[slot_str])
						title_elems=root.findall(xpath+"/Break_Title")
						if(title_elems==[]):
							print('no Break_Title element')
							print('\t',xpath)
							print('\t',day_str, slot_str, type_str, title_str, room_str)
							continue

						for elem in title_elems:
							if elem.text:
								elem.text += text
							else:
								elem.text = text
					continue

				xpath = "./AtAGlance/Day_Table/Table/Cell[@sessionid='day%s_slot%s_room%s']"
				xpath = xpath%( config.indexes[day_str],config.indexes[slot_str],config.indexes[room_str])

				#xpath = "./AtAGlance/Day_Table/Table/Cell[@class='para_session']/Session_Title"
				#• –
				#print(root.tag)
				title_elems=root.findall(xpath+"/Session_Title")
				if(title_elems==[]):
					print('no Session_Title element')
					print('\t',xpath)
					print('\t',day_str, slot_str, type_str, title_str, room_str)
					continue
				#max_length=2000
				#if len(title_str)>max_length:
					#print('"'+title_str+'": '+'"'+title_str+'"')
				#	title_str = title_str.split(':')[0]
				#	title_str = title_str.split('–')[0]
				#	title_str = title_str.split('?')[0]
				#	title_str = title_str.replace('Human-Computer Interaction','HCI')

				#	title_str=title_str[:max_length]

				for title_elem in title_elems:
					title_elem.text=title_str
					if type_str in ['keynote','plenary']:
						for submissionid in session['submissions']:
							paper = papers[submissionid]
							title_elem.text += '. '+paper['title']+'.'

				type_elems=root.findall(xpath+"/Session_Type")
				if(type_elems==[]):
					print('no Session_Type element')
					print('\t',xpath)
					print('\t',day_str, slot_str, type_str, title_str, room_str)
					continue
				for type_elem in type_elems:
					type_elem.text=config.type_actual_str[type_str]

				elems=root.findall(xpath+"/Room_Name")
				for elem in elems:
					elem.text=room_str

				for submissionid in session['submissions']:
					#print(submission)
					paper = papers[submissionid]
					output+="\t\t\t"+paper['title']+'\n'
					authors=[]
					for author in paper['authors']:
						authors.append(author['name'])
					authors_str = ', '.join(authors)
					output+="\t\t\t"+authors_str+'\n'
					output+='\n'

#print(output)
#print('max_session:', max_session)
#print(sorted(allrooms.keys()))
#print(alldays)
#print(allslots)
print(allrooms)
#print(alltypes)

#tree.write('ataglance-01-session-id.xml')
tree.write('ataglance-new.xml')
if shorten_dump:
	open('shorten_session_names.csv','w').write(shorten_session_names_out)


# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Classes for converting between chord progressions and models inputs/outputs.

MajorMinorChordOneHotEncoding is an encoding.OneHotEncoding that specifies a
one-hot encoding for ChordProgression events, i.e. chord symbol strings. This
encoding has 25 classes, all 12 major and minor triads plus "no chord".
"""

# internal imports
from magenta.music import chord_symbols_lib
from magenta.music import constants
from magenta.music import encoder_decoder

NOTES_PER_OCTAVE = constants.NOTES_PER_OCTAVE
NO_CHORD = constants.NO_CHORD

class ChordEncodingException(Exception):
  pass

class MajorMinorChordOneHotEncoding(encoder_decoder.OneHotEncoding):
  """Encodes chords as root + major/minor, with zero index for "no chord".

  Encodes chords as follows:
    0:     "no chord"
    1-12:  chords with a major triad, where 1 is C major, 2 is C# major, etc.
    13-24: chords with a minor triad, where 13 is C minor, 14 is C# minor, etc.
  """

  # Mapping from pitch class index to name.  Eventually this should be defined
  # more globally, but right now only `decode_chord` needs it.
  _PITCH_CLASS_MAPPING = ['C', 'C#', 'D', 'E-', 'E', 'F',
                          'F#', 'G', 'A-', 'A', 'B-', 'B']

  def __init__(self, chord_symbol_functions=
               chord_symbols_lib.ChordSymbolFunctions.get()):
    """Initialize the MajorMinorChordOneHotEncoding object.

    Args:
      chord_symbol_functions: ChordSymbolFunctions object with which to perform
          the actual transposition of chord symbol strings.
    """
    self._chord_symbol_functions = chord_symbol_functions

  @property
  def num_classes(self):
    return 2 * NOTES_PER_OCTAVE + 1

  @property
  def default_event(self):
    return NO_CHORD

  def encode_event(self, event):
    if event == NO_CHORD:
      return 0

    root = self._chord_symbol_functions.chord_symbol_root(event)
    quality = self._chord_symbol_functions.chord_symbol_quality(event)

    if quality == chord_symbols_lib.CHORD_QUALITY_MAJOR:
      return root + 1
    elif quality == chord_symbols_lib.CHORD_QUALITY_MINOR:
      return root + NOTES_PER_OCTAVE + 1
    else:
      raise ChordEncodingException('chord is neither major nor minor: %s'
                                   % event)

  def decode_event(self, index):
    if index == 0:
      return NO_CHORD
    elif index - 1 < 12:
      # major
      return self._PITCH_CLASS_MAPPING[index - 1]
    else:
      # minor
      return self._PITCH_CLASS_MAPPING[index - NOTES_PER_OCTAVE - 1] + 'm'


# -*- coding: utf-8 -*-
from app import utils
from app.model import Base
from app.model.Base import STATUS_INVALID, STATUS_VALID
from peewee import MySQLDatabase, Model, BigIntegerField,CharField, BooleanField, \
                   DecimalField, IntegerField, TextField, DateField, DateTimeField, fn

from playhouse.shortcuts import model_to_dict, dict_to_model
from playhouse.flask_utils import FlaskDB
from datetime import datetime

# 学生管理
class Student(Base.BaseModel):
    class Meta:
        db_table = 'student'

    uid = CharField()
    nickname = CharField()
    name = CharField()
    sex  = IntegerField()
    province = CharField()
    city = CharField()
    country = CharField()
    urgent_contactor = CharField()
    urgent_mobile = CharField()
    birthday = DateField(default='1970-01-01')
    education = CharField()
    image_url = CharField()
    mentor_id = CharField()
    detailed = CharField()
    createtime = DateTimeField(default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    status = IntegerField(default=STATUS_VALID)

def GetStudents(page_num, item_per_page):
    count = Student.select().where(Student.status == STATUS_VALID).count()
    students = []
    print("total record nums: %d" %count)
    #memList = [model_to_dict(mem) for mem in Student.select().order_by(Student.id).paginate(page_num, item_per_page)]
    for mem in Student.select().where(Student.status == STATUS_VALID).order_by(Student.id).paginate(page_num, item_per_page):
        m = model_to_dict(mem)
        m = utils.Time2Str(m)
        m = utils.Decimal2Str(m)
        students.append(m)

    return count, students

def SearchStudentByUid(uid):
    record = Student.get_or_none(Student.uid == uid, Student.status == STATUS_VALID)
    r = model_to_dict(record)
    r = utils.Time2Str(r)
    r = utils.Decimal2Str(r)
    return r

def GetStdInfoById(sid):
    stdinfo = dict()
    record = Student.get_or_none(Student.id == sid, Student.status == STATUS_VALID)
    if record:
        r = model_to_dict(record)
        r = utils.Time2Str(r)
        r = utils.Decimal2Str(r)
        stdinfo['name'] = r['name']
        stdinfo['uid'] = r['uid']
        stdinfo['id'] = r['id']
        stdinfo['nickname'] = r['nickname']
        stdinfo['sex'] = r['sex']
        stdinfo['urgent_contactor'] = r['urgent_contactor']
        stdinfo['urgent_mobile'] = r['urgent_mobile']
        stdinfo['birthday'] = r['birthday']

    return stdinfo

def SearchStudentByName(page_num, item_per_page,name):
    count = Student.select().where(Student.name == name, Student.status == STATUS_VALID).count()
    students = []
    #memList = [model_to_dict(mem) for mem in Student.select().order_by(Student.id).paginate(page_num, item_per_page)]
    for mem in Student.select().where(Student.name == name, Student.status == STATUS_VALID).order_by(Student.id).paginate(page_num, item_per_page):
        m = model_to_dict(mem)
        m = utils.Time2Str(m)
        m = utils.Decimal2Str(m)
        students.append(m)

    return count, students

def GetStudentsInfoByName(name):
    stdsInfo = []
    for mem in Student.select().where(Student.name == name, Student.status == STATUS_VALID).order_by(Student.id):
        m = model_to_dict(mem)
        m = utils.Time2Str(m)
        m = utils.Decimal2Str(m)
        std_name_mobile = m['name'] + '_' + m['urgent_mobile']
        stdInfo = {'value':m['id'], 'label':std_name_mobile }
        stdsInfo.append(stdInfo)

    return stdsInfo

# create student
def CreateStudent(mDict):
    errcode = 1
    print("will create a new record")
    print(mDict)
    m = dict_to_model(Student, mDict)
    m.save()
    mem = Student.get_or_none(Student.nickname == mDict['nickname'], Student.status == STATUS_VALID)
    if mem:
        errcode = 0
    return errcode

def UpdateStudentByUid(mDict):
    errcode = 1
    m = Student.update(**mDict).where(Student.uid==mDict['uid']).execute()
    if m == 1:
        errcode = 0
    return errcode

def DeleteStudentByUid(uid,status=STATUS_INVALID):
    # 0 mean delete successfully
    errcode = 1
    m = Student.update(status=status).where(Student.uid==uid).execute()
    if m == 1:
        errcode = 0
    return errcode

if __name__ == '__main__':
    pass
    """
    create_table()
    for i in range(15):
        Insertstudent()
    #InsertStudent()
    #InsertUser()

    #for i in range(1,4):
    #    Getstudents(i, 5)

    # query student by student id
    mem = SearchstudentById(901527124)
    if mem:
        #print(mem)
        print('%d'%mem.id + "  " + mem.name)
    else:
        print("cannot find this student")

    # query student by memmber name, there may exit many records
    memList = SearchstudentByName('孙宇')
    if memList:
        for mem in memList:
            print("id: %d"%mem.id + "  studentid:%d"%mem.studentid + "  name:" + mem.name)
    else:
        print("cannot find any records")

    # delete a student
    #DeletestudentById(45481440)

    # update a memeber
    #UpdatestudentById(92596066)
    count, memItems = Getstudents(1, 5)
    print("total is : %d"%count)
    print(memItems)
    """


from .simple_cnn import SimpleCNN
from .unet import UnetCNN

def get_cnn(args):
    if args.cnn_name == 'simple':
        return SimpleCNN(args)
    elif args.cnn_name == 'unet':
        return UnetCNN(args)
    else:
        if not args.cnn_name:
            raise Exception(f'You should specify cnn_name via: -c/-cnn or CNN_NAME env variable')
        raise Exception('unknown name {}'.format(args.cnn_name))