text:
# Copyright 2017 The TensorFlow Authors 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.
# ==============================================================================

"""Code for creating sequence datasets.
"""

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

import pickle
from scipy.sparse import coo_matrix
import tensorflow as tf

# The default number of threads used to process data in parallel.
DEFAULT_PARALLELISM = 12

def sparse_pianoroll_to_dense(pianoroll, min_note, num_notes):
    """Converts a sparse pianoroll to a dense numpy array.

    Given a sparse pianoroll, converts it to a dense numpy array of shape
    [num_timesteps, num_notes] where entry i,j is 1.0 if note j is active on
    timestep i and 0.0 otherwise.

    Args:
      pianoroll: A sparse pianoroll object, a list of tuples where the i'th tuple
        contains the indices of the notes active at timestep i.
      min_note: The minimum note in the pianoroll, subtracted from all notes so
        that the minimum note becomes 0.
      num_notes: The number of possible different note indices, determines the
        second dimension of the resulting dense array.
    Returns:
      dense_pianoroll: A [num_timesteps, num_notes] numpy array of floats.
      num_timesteps: A python int, the number of timesteps in the pianoroll.
    """
    num_timesteps = len(pianoroll)
    inds = []
    for time, chord in enumerate(pianoroll):
        # Re-index the notes to start from min_note.
        inds.extend((time, note - min_note) for note in chord)
    shape = [num_timesteps, num_notes]
    values = [1.] * len(inds)
    sparse_pianoroll = coo_matrix(
        (values, ([x[0] for x in inds], [x[1] for x in inds])),
        shape=shape)
    return sparse_pianoroll.toarray(), num_timesteps

def create_pianoroll_dataset(path,
                             split,
                             batch_size,
                             num_parallel_calls=DEFAULT_PARALLELISM,
                             shuffle=False,
                             repeat=False,
                             min_note=21,
                             max_note=108):
    """Creates a pianoroll dataset.

    Args:
      path: The path of a pickle file containing the dataset to load.
      split: The split to use, can be train, test, or valid.
      batch_size: The batch size. If repeat is False then it is not guaranteed
        that the true batch size will match for all batches since batch_size
        may not necessarily evenly divide the number of elements.
      num_parallel_calls: The number of threads to use for parallel processing of
        the data.
      shuffle: If true, shuffles the order of the dataset.
      repeat: If true, repeats the dataset endlessly.
      min_note: The minimum note number of the dataset. For all pianoroll datasets
        the minimum note is number 21, and changing this affects the dimension of
        the data. This is useful mostly for testing.
      max_note: The maximum note number of the dataset. For all pianoroll datasets
        the maximum note is number 108, and changing this affects the dimension of
        the data. This is useful mostly for testing.
    Returns:
      inputs: A batch of input sequences represented as a dense Tensor of shape
        [time, batch_size, data_dimension]. The sequences in inputs are the
        sequences in targets shifted one timestep into the future, padded with
        zeros. This tensor is mean-centered, with the mean taken from the pickle
        file key 'train_mean'.
      targets: A batch of target sequences represented as a dense Tensor of
        shape [time, batch_size, data_dimension].
      lens: An int Tensor of shape [batch_size] representing the lengths of each
        sequence in the batch.
      mean: A float Tensor of shape [data_dimension] containing the mean loaded
        from the pickle file.
    """
    # Load the data from disk.
    num_notes = max_note - min_note + 1
    with tf.gfile.Open(path, "r") as f:
        raw_data = pickle.load(f)
    pianorolls = raw_data[split]
    mean = raw_data["train_mean"]
    num_examples = len(pianorolls)

    def pianoroll_generator():
        for sparse_pianoroll in pianorolls:
            yield sparse_pianoroll_to_dense(sparse_pianoroll, min_note, num_notes)

    dataset = tf.data.Dataset.from_generator(
        pianoroll_generator,
        output_types=(tf.float64, tf.int64),
        output_shapes=([None, num_notes], []))

    if repeat: dataset = dataset.repeat()
    if shuffle: dataset = dataset.shuffle(num_examples)

    # Batch sequences togther, padding them to a common length in time.
    dataset = dataset.padded_batch(batch_size,
                                   padded_shapes=([None, num_notes], []))

    def process_pianoroll_batch(data, lengths):
        """Create mean-centered and time-major next-step prediction Tensors."""
        data = tf.to_float(tf.transpose(data, perm=[1, 0, 2]))
        lengths = tf.to_int32(lengths)
        targets = data
        # Mean center the inputs.
        inputs = data - tf.constant(mean, dtype=tf.float32,
                                    shape=[1, 1, mean.shape[0]])
        # Shift the inputs one step forward in time. Also remove the last timestep
        # so that targets and inputs are the same length.
        inputs = tf.pad(inputs, [[1, 0], [0, 0], [0, 0]], mode="CONSTANT")[:-1]
        # Mask out unused timesteps.
        inputs *= tf.expand_dims(tf.transpose(
            tf.sequence_mask(lengths, dtype=inputs.dtype)), 2)
        return inputs, targets, lengths

    dataset = dataset.map(process_pianoroll_batch,
                          num_parallel_calls=num_parallel_calls)
    dataset = dataset.prefetch(num_examples)

    itr = dataset.make_one_shot_iterator()
    inputs, targets, lengths = itr.get_next()
    return inputs, targets, lengths, tf.constant(mean, dtype=tf.float32)

def create_speech_dataset(path,
                          batch_size,
                          samples_per_timestep=200,
                          num_parallel_calls=DEFAULT_PARALLELISM,
                          prefetch_buffer_size=2048,
                          shuffle=False,
                          repeat=False):
    """Creates a speech dataset.

    Args:
      path: The path of a possibly sharded TFRecord file containing the data.
      batch_size: The batch size. If repeat is False then it is not guaranteed
        that the true batch size will match for all batches since batch_size
        may not necessarily evenly divide the number of elements.
      samples_per_timestep: The number of audio samples per timestep. Used to
        reshape the data into sequences of shape [time, samples_per_timestep].
        Should not change except for testing -- in all speech datasets 200 is the
        number of samples per timestep.
      num_parallel_calls: The number of threads to use for parallel processing of
        the data.
      prefetch_buffer_size: The size of the prefetch queues to use after reading
        and processing the raw data.
      shuffle: If true, shuffles the order of the dataset.
      repeat: If true, repeats the dataset endlessly.
    Returns:
      inputs: A batch of input sequences represented as a dense Tensor of shape
        [time, batch_size, samples_per_timestep]. The sequences in inputs are the
        sequences in targets shifted one timestep into the future, padded with
        zeros.
      targets: A batch of target sequences represented as a dense Tensor of
        shape [time, batch_size, samples_per_timestep].
      lens: An int Tensor of shape [batch_size] representing the lengths of each
        sequence in the batch.
    """
    filenames = [path]

    def read_speech_example(value):
        """Parses a single tf.Example from the TFRecord file."""
        decoded = tf.decode_raw(value, out_type=tf.float32)
        example = tf.reshape(decoded, [-1, samples_per_timestep])
        length = tf.shape(example)[0]
        return example, length

    # Create the dataset from the TFRecord files
    dataset = tf.data.TFRecordDataset(filenames).map(
        read_speech_example, num_parallel_calls=num_parallel_calls)
    dataset = dataset.prefetch(prefetch_buffer_size)

    if repeat: dataset = dataset.repeat()
    if shuffle: dataset = dataset.shuffle(prefetch_buffer_size)

    dataset = dataset.padded_batch(
        batch_size, padded_shapes=([None, samples_per_timestep], []))

    def process_speech_batch(data, lengths):
        """Creates Tensors for next step prediction."""
        data = tf.transpose(data, perm=[1, 0, 2])
        lengths = tf.to_int32(lengths)
        targets = data
        # Shift the inputs one step forward in time. Also remove the last timestep
        # so that targets and inputs are the same length.
        inputs = tf.pad(data, [[1, 0], [0, 0], [0, 0]], mode="CONSTANT")[:-1]
        # Mask out unused timesteps.
        inputs *= tf.expand_dims(
            tf.transpose(tf.sequence_mask(lengths, dtype=inputs.dtype)), 2)
        return inputs, targets, lengths

    dataset = dataset.map(process_speech_batch,
                          num_parallel_calls=num_parallel_calls)
    dataset = dataset.prefetch(prefetch_buffer_size)

    itr = dataset.make_one_shot_iterator()
    inputs, targets, lengths = itr.get_next()
    return inputs, targets, lengths

reason: from scipy


text:
import numpy as np
import cv2 as cv
from skimage import io
from google.colab.patches import cv2_imshow

url = "https://drive.google.com/uc?id=1tbNvzsQRtLSgbeuW3DwBmq683lcLCM-g"

myimg2 = io.imread(url)
myimg5 = cv.cvtColor(myimg2,cv.COLOR_BGR2RGB)

cv2_imshow(myimg5)

boundry = [([0,0,150], [100,100,255]),
           ([0,150,0], [100,255,100]),
           ([150,0,0], [255,100,100])]

for (lower,upper) in boundry:
  lower = np.array(lower, dtype = "uint8")
  upper = np.array(upper, dtype = "uint8")
  mask = cv.inRange(myimg5, lower,upper)
  cv2_imshow(mask)
  img = cv.bitwise_and(myimg5,myimg5,mask = mask)
  cv2_imshow(img)

ext: py

max_stars_repo_name: sreeharijp/ColorMask

max_issues_repo_name: sreeharijp/ColorMask

max_forks_repo_name: sreeharijp/ColorMask

reason: import numpy


text:
import numpy as np
import mxnet as mx
import random
from PIL import Image, ImageFilter
from PIL import ImageFile
import cv2
import sys, os
from mxnet.io import DataIter
from symbol_vgg16 import get_vgg16_rpn
import math

ImageFile.LOAD_TRUNCATED_IMAGES = True

class position:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.width = 0
        self.height = 0

    def in_box(self, a, b):
        return a >= self.x and a < self.x + self.width and \
               b >= self.y and b < self.y + self.height

class feature_pos:
    def __init__(self, arr):
        self.x, self.y, self.pts_x, self.pts_y = [int(float(item)) for item in arr]

class Face:
    def __init__(self):
        self.num_keypoint = 0
        self.bbox = position()
        self.proj_bbox = position()
        self.keypoints = {}
        self.ellipse = []

class myImage:
    def __init__(self):
        self.filename = ''
        self.width = 0
        self.height = 0
        self.image = None
        self.faces = {}
        self.num_faces = 0
        self.resize_factor = 0

class FileIter(DataIter):
    def __init__(self, file_lst, class_names, file_mface,
                 rgb_mean=(117, 117, 117),
                 data_name="data",
                 cls_label_name="cls_label",
                 proj_label_name="proj_label",
                 proj_weight_name="proj_weight",
                 ground_truth_name="ground_truth",
                 bbox_label_name="bbox_label",
                 bbox_weight_name="bbox_weight",
                 bgfg=False,
                 num_data_used=-1):
        self.num_data_used = num_data_used
        self.bgfg = bgfg
        self.num_class = 10
        super(FileIter, self).__init__()
        self.file_lst = file_lst
        f_class = open(class_names, 'r')
        count = 1
        self.class_names = {}
        self.class_names[0] = "background"
        for line in f_class:
            self.class_names[line.strip('\n')] = count
            self.class_names[count] = line.strip('\n')
            count += 1
        self.rgb_mean = np.array(rgb_mean)
        f_class.close()

        self.mean_face = np.zeros([self.num_class, 3], np.float32)
        f_mface = open(file_mface, 'r')
        for line in f_mface:
            line = line.strip('\n').split(' ')
            xyz = np.array([float(line[1]), float(line[2]), float(line[3])])
            self.mean_face[self.class_names[line[0]] - 1] = xyz
        f_mface.close()
        self.AllImg = {}

        self.data_name = data_name
        self.cls_label_name = cls_label_name
        self.proj_label_name = proj_label_name
        self.proj_weight_name = proj_weight_name
        self.ground_truth_name = ground_truth_name
        self.bbox_label_name = bbox_label_name
        self.bbox_weight_name = bbox_weight_name
        self.num_data = len(open(self.file_lst, 'r').readlines())
        self.f = open(self.file_lst, 'r')
        self.cursor = -1
        self.data, self.cls_label, self.proj_label, self.proj_weight, self.ground_truth,\
            self.bbox_label, self.bbox_weight = [], [], [], [], [], [], []

    #####################

    def _read(self):
        self.AllImg[self.cursor] = myImage()
        line = self.f.readline()
        onedata = line.strip('\n').split(' ')
        imgFilename = onedata[1]

        self.AllImg[self.cursor].filename = imgFilename
        img_width, img_height, resize_factor = [float(item) for item in onedata[2:5]]
        wd_resize = int(img_width * resize_factor)
        ht_resize = int(img_height * resize_factor)
        self.AllImg[self.cursor].width = wd_resize
        self.AllImg[self.cursor].height = ht_resize
        self.AllImg[self.cursor].resize_factor = resize_factor
        img = Image.open(imgFilename).convert("RGB")
        if_blur = random.randint(1, 4)
        blur_radius = random.randint(5, 20)
        if if_blur == 1:
            print 'Blured: ', blur_radius
            img = img.filter(ImageFilter.GaussianBlur(blur_radius))
        img = img.resize((wd_resize, ht_resize))
        img = np.array(img, dtype=np.float32)

        self.AllImg[self.cursor].image = []
        num_faces = int(onedata[5])
        num_keypoints = 0
        self.AllImg[self.cursor].num_faces = num_faces
        faces = {}
        onedata = onedata[6:]
        for j in range(0, num_faces):
            faces[j], num_k, onedata = self._read_oneface(onedata)
            num_keypoints += num_k
        self.AllImg[self.cursor].faces = faces

        tempt_model = get_vgg16_rpn()
        out_height, out_width = tempt_model.infer_shape(data=(1, 3, ht_resize, wd_resize), mean_face=(10, 3),
                                                        ground_truth=(10, 2), bbox_label=(10, 5))[1][0][2:4]
        cls_label = 255 * np.ones((out_height, out_width), dtype=np.int32)
        proj_label = np.zeros((out_height, out_width, self.num_class, 2), dtype=np.int32)
        proj_weight = np.zeros((out_height, out_width, self.num_class, 2), dtype=np.float32)
        ground_truth = np.zeros((num_keypoints * 9, 2), dtype=np.int32)
        bbox_label = np.zeros((num_keypoints * 9, 5), dtype=np.float32)
        bbox_weight = np.zeros((num_keypoints * 9, 5), dtype=np.float32)

        ratio_w, ratio_h = float(out_width) / float(wd_resize), float(out_height) / float(ht_resize)
        count = 0

        flag_map = np.ones((out_height, out_width))  # used to record negative anchors selection

        gt_counter = 0
        for i in range(0, num_faces):
            kp_arr = np.zeros((self.num_class, 2), dtype=np.int32)
            kp_warr = np.zeros((self.num_class, 2), dtype=np.float32)
            for kname, kp in faces[i].keypoints.iteritems():
                kp_arr[self.class_names[kname] - 1] = np.array([kp.y, kp.x])
                kp_warr[self.class_names[kname] - 1] = \
                    np.array([100.0 / faces[i].bbox.height, 100.0 / faces[i].bbox.height])
            for kname, kp in faces[i].keypoints.iteritems():
                for idx_y in range(-1, 2):
                    if (kp.y + idx_y) * ratio_h >= 0 and (kp.y + idx_y) * ratio_h < out_height:
                        for idx_x in range(-1, 2):
                            if (kp.x + idx_x) * ratio_w >= 0 and (kp.x + idx_x) * ratio_w < out_width:
                                ground_truth[gt_counter, :] = np.array([kp.y + idx_y, kp.x + idx_x])
                                bbox_label[gt_counter, :] = np.array([faces[i].ellipse[0],
                                                                      faces[i].ellipse[1],
                                                                      faces[i].ellipse[3],
                                                                      faces[i].ellipse[4],
                                                                      faces[i].ellipse[2]])
                                bbox_weight[gt_counter, :] = np.array([1.0, 1.0, 1.0, 1.0, 1.0])
                                gt_counter += 1

            for kname, kp in faces[i].keypoints.iteritems():
                pos_x, pos_y = int(kp.x * ratio_w), int(kp.y * ratio_h)

                if pos_x > out_width or pos_y > out_height:
                    print self.AllImg[self.cursor].filename
                    print pos_x, pos_y, out_width, out_height, wd_resize, ht_resize, ratio_w, ratio_h
                count += 1
                for idx_y in range(-1, 2):
                    if pos_y + idx_y >= 0 and pos_y + idx_y < out_height:
                        for idx_x in range(-1, 2):
                            if pos_x + idx_x >= 0 and pos_x + idx_x < out_width:
                                if self.bgfg:
                                    cls_label[pos_y + idx_y, pos_x + idx_x] = 1
                                else:
                                    cls_label[pos_y + idx_y, pos_x + idx_x] = self.class_names[kname]
                                proj_label[pos_y + idx_y, pos_x + idx_x] = kp_arr
                                proj_weight[pos_y + idx_y, pos_x + idx_x] = kp_warr

        for i in range(0, num_faces):
            bbox = faces[i].bbox
            y = int(max(0, math.floor(bbox.y * ratio_h)))
            ty = int(min(out_height - 1, math.floor((bbox.y + bbox.height) * ratio_h)))
            x = int(max(0, math.floor(bbox.x * ratio_w)))
            tx = int(min(out_width - 1, math.floor((bbox.x + bbox.width) * ratio_w)))
            flag_map[y: ty, x: tx] = np.zeros((ty - y, tx - x))

        # random choose negative anchors
        for i in range(0, 9 * count):
            left_anchor = np.nonzero(flag_map)
            if left_anchor[0].size:
                index_neg = random.randint(0, left_anchor[0].size - 1)
                cls_label[left_anchor[0][index_neg], left_anchor[1][index_neg]] = 0
                flag_map[left_anchor[0][index_neg], left_anchor[1][index_neg]] = 0
            else:
                break

        img = img - self.rgb_mean
        img = np.swapaxes(img, 0, 2)
        img = np.swapaxes(img, 1, 2)  # (c,h,w)
        img = np.expand_dims(img, axis=0)  # (1,c,h,w) 1 is batch number

        cls_label = np.expand_dims(cls_label, axis=0)
        proj_label = np.expand_dims(proj_label, axis=0)
        proj_weight = np.expand_dims(proj_weight, axis=0)

        return img, cls_label, proj_label, proj_weight, ground_truth[:gt_counter], \
               bbox_label[:gt_counter], bbox_weight[:gt_counter]

    def _read_oneface(self, str):
        oneface = Face()
        # every face has bouding box and proj bounding box
        oneface.bbox.x, oneface.bbox.y, oneface.bbox.width, oneface.bbox.height, \
        oneface.proj_bbox.x, oneface.proj_bbox.y, oneface.proj_bbox.width, oneface.proj_bbox.height = [
            int(float(item)) for item in str[0:8]]

        num_keypoint = int(str[8])
        oneface.num_keypoint = num_keypoint

        # every keypoint has name, x, y proj_x, proj_y
        for i in range(0, num_keypoint):
            info_kp = str[9 + i * 5:9 + (i + 1) * 5]
            name_kp = info_kp[0]
            oneface.keypoints[name_kp] = feature_pos(info_kp[1:5])

        ellipse = str[9 + num_keypoint * 5: 14 + num_keypoint * 5]
        oneface.ellipse = np.array([float(item) for item in ellipse])
        if oneface.ellipse[2] < 0:
            oneface.ellipse[2] += np.pi
        return oneface, num_keypoint, str[26 + num_keypoint * 5:]

    def get_batch_size(self):
        return 1

    def reset(self):
        self.cursor = -1
        self.f.close()
        self.f = open(self.file_lst, 'r')

    def iter_next(self):
        self.cursor += 1
        if self.num_data_used != -1 and self.cursor > self.num_data_used:
            return False
        if (self.cursor < self.num_data):
            return True
        else:
            return False

    def next(self):
        # return a dictionary contains all data needed for one iteration
        if self.iter_next():
            self.data, self.cls_label, self.proj_label, self.proj_weight, self.ground_truth,\
                self.bbox_label, self.bbox_weight = self._read()
            return {self.data_name: self.data,
                    self.cls_label_name: self.cls_label,
                    self.proj_label_name: self.proj_label,
                    self.proj_weight_name: self.proj_weight,
                    self.ground_truth_name: self.ground_truth,
                    self.bbox_label_name: self.bbox_label,
                    self.bbox_weight_name: self.bbox_weight}
        else:
            raise StopIteration


text:
"""
Used to draw R-D curves

data format:
bpp, ms-ssim(db), psnr(db)

"""
import os
import argparse
import numpy as np

from fjcommon import functools_ext as ft
from fjcommon.iterable_ext import flag_first as flag_first_iter
import matplotlib as mpl
mpl.rcParams['text.latex.unicode'] = True
mpl.use('Agg')  # No display
from matplotlib import pyplot as plt

import constants
from codes_distance import get_interpolated_values_bpg_jp2k, get_measures_readers, interpolate_ours, \
    DEFAULT_BPP_GRID, CODECS

plt.rcParams['font.sans-serif'] = ['SimHei'] # # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# mssim_path
BPG420_kodak_pth = "../results/image_compression_baselines/kodak/MS-SSIM_sRGB_RGB/bpg420.txt"
BPG444_kodak_pth = "../results/image_compression_baselines/kodak/MS-SSIM_sRGB_RGB/bpg444.txt"
JP2K_kodak_pth = "../results/image_compression_baselines/kodak/MS-SSIM_sRGB_RGB/j2k-kdu5.txt"
Jpeg_kodak_pth = "../results/image_compression_baselines/kodak/MS-SSIM_sRGB_RGB/jpeg420.txt"
WebP_kodak_pth = "../results/image_compression_baselines/kodak/MS-SSIM_sRGB_RGB/webp.txt"

balle_2018_opt_mse_pth = "../results/image_compression_baselines/kodak/MS-SSIM_sRGB_RGB/balle-2018-iclr-opt-mse.txt"

# baseline_GMM_pth = "../results/our_proposed/kodak/proposed_GMM_opt_mse.txt"
Proposed_pth = "../results/our_proposed/kodak/proposed_opt_mse_msssim.txt"

# ----------------------------
# psnr_path
# BPG420_kodak_pth = "../results/image_compression_baselines/kodak/PSNR_sRGB_RGB/bpg420.txt"
# BPG444_kodak_pth = "../results/image_compression_baselines/kodak/PSNR_sRGB_RGB/bpg444.txt"
# JP2K_kodak_pth = "../results/image_compression_baselines/kodak/PSNR_sRGB_RGB/j2k-kdu5.txt"
# Jpeg_kodak_pth = "../results/image_compression_baselines/kodak/PSNR_sRGB_RGB/jpeg420.txt"
# WebP_kodak_pth = "../results/image_compression_baselines/kodak/PSNR_sRGB_RGB/webp.txt"
#
# balle_2018_opt_mse_pth = "../results/image_compression_baselines/kodak/PSNR_sRGB_RGB/balle-2018-iclr-opt-mse.txt"
#
# Proposed_pth = "../results/our_proposed/kodak/proposed_opt_mse_psnr.txt"

# ----------------------------
# ablation_study
# baseline_pth = '../results/our_proposed/Ablation_study/baseline.txt'
# baseline_GMM_pth = '../results/our_proposed/Ablation_study/baseline+GMM.txt'
# baseline_GMM_CAM_pth = '../results/our_proposed/Ablation_study/baseline+GMM+attention.txt'

LABEL_BPG = 'BPG'
LABEL_JP2K = 'JPEG2000'
LABEL_JP = 'JPEG'
LABEL_WEBP = 'WebP'
LABEL_BPG_420 = "BPG(4:2:0)"
LABEL_BPG_444 = "BPG(4:4:4)"
LABEL_BALLE_MSE = 'Ballé[MSE][ICLR18]'
LABEL_BALLE_MSSSIM = 'Ballé[MS-SSIM][ICLR18]'
LABEL_proposed_GMM_opt_mse = 'Baseline with GMM'
LABEL_proposed_opt_mse = '本文方法'
LABEL_baseline = 'Baseline'
LABEL_baseline_GMM = 'Baseline+GMM'
LABEL_baseline_GMM_GCAM = 'Baseline+GMM+GCAM'

TITLES = {'u100': 'Urban100',
          'b100': 'B100',
          'rf100': 'ImageNetVal',
          'kodak': 'Kodak',
          'testset': 'TestSet'}

def read_resultTXT(file_pth):
	out = []
	with open(file_pth, 'r') as fr:
		for line in fr.readlines():
			if line[0].isalnum() is True:
				line = line.strip().split(',')
				out.append([float(ch) for ch in line])
			else:
				continue
	return out

def get_label_from_codec_short_name(codec_short_name):
    # 'bpg': LABEL_BPG,
    # 'jp2k': LABEL_JP2K,
    # 'jp': LABEL_JP,
    # 'webp': LABEL_WEBP,
    return {
            'bpg420':   LABEL_BPG_420,
            'bpg444':   LABEL_BPG_444,
            'j2k-kdu5': LABEL_JP2K,
            'jpeg420':  LABEL_JP,
            'webp':     LABEL_WEBP,
            'balle-2018-iclr-opt-mse':         LABEL_BALLE_MSE,
            'balle-2018-iclr-opt-msssim':      LABEL_BALLE_MSSSIM,
            'proposed_GMM_opt_mse':            LABEL_proposed_GMM_opt_mse,
            'proposed_opt_mse_msssim':         LABEL_proposed_opt_mse,
            'proposed_opt_mse_psnr':           LABEL_proposed_opt_mse,
            'baseline':                        LABEL_baseline,
            'baseline+GMM':                    LABEL_baseline_GMM,
            'baseline+GMM+attention':          LABEL_baseline_GMM_GCAM}[codec_short_name]

# style = {
#         LABEL_BPG_420: (cmap(0.7), '-', 1.5)
#         # LABEL_OURS: ('0', '-', 3),
#         # LABEL_RB: (cmap(0.9), '-', 1.5),
#         # LABEL_BPG: (cmap(0.7), '-', 1.5),
#         # LABEL_JP2K: (cmap(0.45), '-', 1.5),
#         # LABEL_JP: (cmap(0.2), '-', 1.5),
#         # LABEL_WEBP: (cmap(0.6), '-', 1.5),
#         # LABEL_JOHNSTON: (cmap(0.7), '--', 1.5),
#         # LABEL_BALLE: (cmap(0.45), '--', 1.5),
#         # LABEL_THEIS: (cmap(0.2), '--', 1.5),
#         # LABEL_Our: (cmap(0.2), '-', 1.5)
#     }

def plot_(measure_pth, style, evaluate='ms-ssim'):
    label = os.path.basename(measure_pth).split('.')[0] # get filename without extension name
    label = get_label_from_codec_short_name(label)
    col, line_style, line_width, marker = style[label]
    dashes = (5, 1) if line_style == '--' else [] # i do not know that dashes means

    metric = np.array(read_resultTXT(measure_pth))
    this_bpp, this_metric = metric[:, 0], metric[:, 1]
    # ms_ssim_db = -10 * log10(1 - ms_ssim)
    if evaluate == 'ms-ssim':
        this_metric = -10 * np.log10(1 - this_metric)
    plt.plot(this_bpp, this_metric, label=label, linewidth=line_width, color=col, dashes=dashes, marker=marker, ms=3)
    # plt.savefig('test_tmp.png')
#
# def plot_dot():
# 	pass

def interpolate_curve(x_range, y_range, output_path=None):
    if not output_path:
        output_path = 'tmp.png'

    plt.figure(figsize=(6, 6))
    # cmap = plt.cm.get_cmap('cool')
    cmap = plt.cm.get_cmap('tab20')

# -------------------------------------------
    # color, line_style, line_width, marker
    style = {
        LABEL_BPG_420: (cmap(1),  '-', 1.5, ''),
        LABEL_BPG_444: (cmap(2),  '-', 1.5, ''),
        LABEL_JP2K:    (cmap(3), '-', 1.5, ''),
        LABEL_JP:      (cmap(4),  '-', 1.5, ''),
        LABEL_WEBP:    (cmap(5),  '-', 1.5, ''),
        LABEL_BALLE_MSE: (cmap(6), '--', 1.5, ''),
        LABEL_BALLE_MSSSIM: (cmap(7), '--', 1.5, ''),
        LABEL_proposed_GMM_opt_mse: (cmap(8), '-', 1.5, 'o'),
        LABEL_proposed_opt_mse: (cmap(9), '-', 1.5, 'o'),
        LABEL_baseline: (cmap(10), '-', 1.5, 'o'),
        LABEL_baseline_GMM: (cmap(11), '-', 1.5, 'o'),
        LABEL_baseline_GMM_GCAM: (cmap(12), '-', 1.5, 'o')
    }

    pos = {
        LABEL_BPG_420: 1,
        LABEL_BPG_444: 2,
        LABEL_JP2K:    3,
        LABEL_JP:      4,
        LABEL_WEBP:    5,
        LABEL_BALLE_MSE: 6,
        LABEL_BALLE_MSSSIM: 7,
        LABEL_proposed_GMM_opt_mse: 8,
        LABEL_proposed_opt_mse: 9,
        LABEL_baseline: 10,
        LABEL_baseline_GMM: 11,
        LABEL_baseline_GMM_GCAM: 12
    }

    plt.figure(figsize=(6, 6))

    # BPG(4:2:0)
    plot_(BPG420_kodak_pth, style)
    # BPG(4:4:0)
    plot_(BPG444_kodak_pth, style)
    # JP2k
    plot_(JP2K_kodak_pth, style)
    # JPEG
    plot_(Jpeg_kodak_pth, style)
    # WebP
    plot_(WebP_kodak_pth, style)
    # Balle 2018 opt mse
    plot_(balle_2018_opt_mse_pth, style)
    # Balle 2018 opt msssim
    # plot_(balle_2018_msssim_opt_msssim_pth, style)

    # plot_(baseline_GMM_pth, style)
    plot_(Proposed_pth, style)

    # ablation study
    # plot_(baseline_pth, style, evaluate=None)
    # plot_(baseline_GMM_pth, style, evaluate=None)
    # plot_(baseline_GMM_CAM_pth, style, evaluate=None)

    # plt.title('{} on {}'.format('ms-ssim'.upper(), 'Kodak'))
    # plt.title('{} on {}'.format('psnr'.upper() + '(dB)', 'Kodak'))
    plt.xlabel('码率/bpp', labelpad=-1)
    # plt.ylabel('{} on {}'.format('ms-ssim'.upper() + '(dB)', 'Kodak'))
    plt.ylabel('{}/{}'.format('ms-ssim'.upper(),'dB'))
    plt.grid()

# ------------------------------------------------

    ax = plt.gca()
    handles, labels = ax.get_legend_handles_labels()
    labels, handles = zip(*sorted(zip(labels, handles), reverse=True, key=lambda t: pos[t[0]]))
    ax.legend(handles, labels, loc=4, prop={'size': 12}, fancybox=True, framealpha=0.7)

    ax.yaxis.grid(b=True, which='both', color='0.8', linestyle='-')
    ax.xaxis.grid(b=True, which='major', color='0.8', linestyle='-')
    ax.set_axisbelow(True)

    ax.minorticks_on()
    ax.yaxis.set_minor_locator(mpl.ticker.AutoMinorLocator(2))

    plt.xlim(x_range)
    plt.ylim(y_range)
    plt.xticks(np.arange(x_range[0], x_range[1], 0.1)) # 0.1
    plt.yticks(np.arange(y_range[0], y_range[1], 2)) # 2
    print('Saving {}...'.format(output_path))
    plt.savefig(output_path, bbox_inches='tight')
    plt.close()

def main():
    p = argparse.ArgumentParser()
    p.add_argument('--log_dir_root', help='Path to dir containing log_dirs.')
    p.add_argument('--job_ids', help='Comma separated list of job_ids.')
    p.add_argument('images')
    p.add_argument('--x_range', default='0,1.2')
    p.add_argument('--y_range', default='0.85,1.0')
    p.add_argument('--latex', action='store_true')
    p.add_argument('--output_path', '-o', help='Path to store plot. Defaults to plot_DATASET.png.')
    p.add_argument('--style', nargs='+', default=['interp'], choices=['interp', 'mean'])
    p.add_argument('--paper_plot', action='store_true')
    p.add_argument('--ids', help='If given with --style mean, label mean points with these ids.',
                   nargs='+')
    flags = p.parse_args()

    range_to_floats = lambda r: tuple(map(float, r.split(',')))
    interpolated_curve(flags.log_dir_root, flags.job_ids, flags.images,
                       DEFAULT_BPP_GRID, 'quadratic',
                       plot_interp_of_ours='interp' in flags.style,
                       plot_mean_of_ours='mean' in flags.style,
                       plot_ids_of_ours=flags.ids,
                       metric='ms-ssim',
                       x_range=range_to_floats(flags.x_range), y_range=range_to_floats(flags.y_range),
                       use_latex=flags.latex,
                       output_path=flags.output_path,
                       paper_plot=flags.paper_plot)

if __name__ == '__main__':
    # psnr x : 0.1, 1.7
    # ms-ssim x :0.1, 1.8
    interpolate_curve(x_range=(0.1, 1.8), y_range=(10, 26)) # (10, 26) for msssim, (20, 43) for psnr
    # main()


text:
#  Copyright 2020 Nick Guletskii
#
#  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.

from abc import ABC, abstractmethod

import numpy as np
import onnx
from caffe2.python.onnx import backend as backend

from ori3nt8.utils.resources import resource_path

class AbstractExecutor(ABC):
    @abstractmethod
    def __call__(self, images: np.ndarray) -> np.ndarray:
        raise NotImplementedError()

class Caffe2Executor():
    def __init__(self):
        onnx_model = onnx.load(str(resource_path() / "network.onnx"))
        self.rep = backend.prepare(onnx_model, device="CPU")

    def __call__(self, images: np.ndarray) -> np.ndarray:
        results = self.rep.run(images)
        return np.squeeze(results, axis=0)


text:
import numpy as np
from oolearning.splitters.StratifiedDataSplitter import StratifiedDataSplitter

class ClassificationStratifiedDataSplitter(StratifiedDataSplitter):
    """
    Splits the data into training/holdout sets while maintaining the categorical proportions of the
    target variable
    """

    def labels_to_stratify(self, target_values: np.ndarray) -> np.ndarray:
        return target_values  # for classification, we are just going to use the target variables as is.

max_stars_repo_name: shane-kercheval/oo-learning

max_issues_repo_name: shane-kercheval/oo-learning

max_forks_repo_name: shane-kercheval/oo-learning


text:
import argparse
import logging
import sys
import time

from tf_pose import common
import cv2
import numpy as np
from tf_pose.estimator import TfPoseEstimator
from tf_pose.networks import get_graph_path, model_wh

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='tf-pose-estimation run')
    parser.add_argument('--image', type=str, default='./images/p1.jpg')
    parser.add_argument('--model', type=str, default='mobilenet_thin', help='cmu / mobilenet_thin')
    args = parser.parse_args()

    e = TfPoseEstimator(get_graph_path(args.model), target_size=(432, 368))

    # estimate human poses from a single image !
    image = common.read_imgfile(args.image, None, None)
    humans = e.inference(image, resize_to_default=False, upsample_size=4)
    image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)

    import matplotlib.pyplot as plt

    fig = plt.figure()
    a = fig.add_subplot(2, 2, 1)
    a.set_title('Result')
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

    bgimg = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2RGB)
    bgimg = cv2.resize(bgimg, (e.heatMat.shape[1], e.heatMat.shape[0]), interpolation=cv2.INTER_AREA)

    # show network output
    a = fig.add_subplot(2, 2, 2)
    plt.imshow(bgimg, alpha=0.5)
    tmp = np.amax(e.heatMat[:, :, :-1], axis=2)
    plt.imshow(tmp, cmap=plt.cm.gray, alpha=0.5)
    plt.colorbar()

    tmp2 = e.pafMat.transpose((2, 0, 1))
    tmp2_odd = np.amax(np.absolute(tmp2[::2, :, :]), axis=0)
    tmp2_even = np.amax(np.absolute(tmp2[1::2, :, :]), axis=0)

    a = fig.add_subplot(2, 2, 3)
    a.set_title('Vectormap-x')
    # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5)
    plt.imshow(tmp2_odd, cmap=plt.cm.gray, alpha=0.5)
    plt.colorbar()

    a = fig.add_subplot(2, 2, 4)
    a.set_title('Vectormap-y')
    # plt.imshow(CocoPose.get_bgimg(inp, target_size=(vectmap.shape[1], vectmap.shape[0])), alpha=0.5)
    plt.imshow(tmp2_even, cmap=plt.cm.gray, alpha=0.5)
    plt.colorbar()
    plt.show()


text:
"""
Fourier Transforms

The frequency components of an image can be displayed after doing a Fourier Transform (FT).
An FT looks at the components of an image (edges that are high-frequency, and areas of smooth
color as low-frequency), and plots the frequencies that occur as points in spectrum.

In fact, an FT treats patterns of intensity in an image as sine waves with a particular frequency,
and you can look at an interesting visualization of these sine wave components on this page.

We'll first look at a few simple image patterns to build up an idea of what image frequency
components look like, and then transform a more complex image to see what it looks like in the frequency domain.

"""

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

# Read in the images
image_stripes = cv2.imread('images/stripes.jpg')
image_solid = cv2.imread('images/pink_solid.jpg')

# Change color to RGB (from BGR)
image_stripes = cv2.cvtColor(image_stripes, cv2.COLOR_BGR2RGB)
image_solid = cv2.cvtColor(image_solid, cv2.COLOR_BGR2RGB)

# Display the images
f, (ax1,ax2) = plt.subplots(1, 2, figsize=(10,5))

ax1.imshow(image_stripes)
ax2.imshow(image_solid)

# convert to grayscale to focus on the intensity patterns in the image
gray_stripes = cv2.cvtColor(image_stripes, cv2.COLOR_RGB2GRAY)
gray_solid = cv2.cvtColor(image_solid, cv2.COLOR_RGB2GRAY)

# normalize the image color values from a range of [0,255] to [0,1] for further processing
norm_stripes = gray_stripes / 255.0
norm_solid = gray_solid / 255.0

# perform a fast fourier transform and create a scaled, frequency transform image
def ft_image(norm_image):
    '''
        This function takes in a normalized, grayscale image
        and returns a frequency spectrum transform of that image.
    '''
    f = np.fft.fft2(norm_image)
    fshift = np.fft.fftshift(f)
    frequency_tx = 20 * np.log(np.abs(fshift))

    return frequency_tx

f_stripes = ft_image(norm_stripes)
f_solid = ft_image(norm_solid)

# display the images
# original images to the left of their frequency transform
f, (ax1,ax2,ax3,ax4) = plt.subplots(1, 4, figsize=(20,10))

ax1.set_title('original image')
ax1.imshow(image_stripes)
ax2.set_title('frequency transform image')
ax2.imshow(f_stripes, cmap='gray')

ax3.set_title('original image')
ax3.imshow(image_solid)
ax4.set_title('frequency transform image')
ax4.imshow(f_solid, cmap='gray')

"""
Low frequencies are at the center of the frequency transform image.
The transform images for these example show that the solid image has most
low-frequency components (as seen by the center bright spot).

The stripes transform image contains low-frequencies for the areas of white
and black color and high frequencies for the edges in between those colors.
The stripes transform image also tells us that there is one dominating direction
for these frequencies; vertical stripes are represented by a horizontal line passing
through the center of the frequency transform image.
"""

# Read in an image
image = cv2.imread('images/birds.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
norm_image = gray/255.0

f_image = ft_image(norm_image)
f, (ax1,ax2) = plt.subplots(1, 2, figsize=(20,10))

ax1.imshow(image)
ax2.imshow(f_image, cmap='gray')
"""
This image has components of all frequencies. You can see a bright spot in the center
of the transform image, which tells us that a large portion of the image is low-frequency;
this makes sense since the body of the birds and background are solid colors. The transform
image also tells us that there are two dominating directions for these frequencies;
vertical edges (from the edges of birds) are represented by a horizontal line passing through
the center of the frequency transform image, and horizontal edges (from the branch and tops
of the birds' heads) are represented by a vertical line passing through the center.
"""

max_stars_repo_name: aramki/computervisionBasics

max_issues_repo_name: aramki/computervisionBasics

max_forks_repo_name: aramki/computervisionBasics


text:
import mdtraj as mdtraj
import numpy as np

STRUCTURE = ["C", "H", "B", "E", "G", "I", "T", "S", " "]

def deg_to_rad(deg):
    return deg * np.pi / 180

def rad_to_deg(rad):
    return rad * 180 / np.pi

def compute_structure(res_dssp):
    list_structure = []
    for i in range(res_dssp.shape[1]):
        dico_structure = count_structure(res_dssp[:,i])
        list_structure.append(dico_structure)
    return list_structure

def count_structure(arr):
    dic_count = {}
    for c in arr:
        if c not in dic_count.keys():
            dic_count[c] = 1
        else:
            dic_count[c] += 1
    return dic_count

def get_consensus_structure(dic_count_structure):
    consensus_struct = []
    for i in dic_count_structure:
        if len(i.keys()) == 1:
            consensus_struct.append(i.keys()[0])
        else :
            keyMax = keywithmaxval(i)
            if consensus_struct[-1] in keyMax:
                consensus_struct.append(consensus_struct[-1])
    return consensus_struct

def keys_with_max_val(d):
    """ a) create a list of the dict's keys and values;
        b) return the key with the max value"""
    v=list(d.values())
    nbMax =  v.count(max(v))
    k=list(d.keys())
    if nbMax == 1:
        return k[v.index(max(v))]
    else:
        keys = []
        for i in range(nbMax):
            v=list(d.values())
            k=list(d.keys())
            firstKey = k[v.index(max(v))]
            keys.append(firstKey)
            del d[firstKey]
        return keys

def count_structure(consensus_structure):
    dic_count= {}
    for struct in STRUCTURE :
        dic_count[struct] = consensus_structure.count(struct)
    return dic_count

def get_helicity(consensus_structure):
    dic_count_struct = count_structure(consensus_structure)
    len_prot = sum(dic_count_struct.values())
    aa_in_helix = dic_count_struct["H"]
    helicity = aa_in_helix * 100. / len_prot
    return round(helicity,0)

def get_helices(struct):
    nb_h = 0
    #after this threshold the helix is taken into account
    threshold = 4
    dic_struct = {}
    for i in range(len(struct)):
        if struct[i] != struct[i-1]:
            if struct[i-1] in ["H", "G", "I"]:
                dic_struct[struct[i-1]][-1][1] = i-1
            nb_h =1
        else :
            nb_h +=1
            if nb_h == threshold :
                if struct[i] not in dic_struct:
                    dic_struct[struct[i]] = [[i-3, 0]]
                else:
                    dic_struct[struct[i]].append([i-3, 0])
    return dic_struct

def mean(list_angle):
    #Faire la somme de tous les angles
    #puis faire une moyenne
    sum_tot = 0
    length = 0
    for ang in list_angle:
        sum_tot += sum(ang)
        length += len(ang)
    return sum_tot / length

# en degré
def get_phi_psi(traj, dic_helix):
    dic_phsi ={}
    for key in dic_helix.keys():
        for j in range(len(dic_helix[key])):
            start = dic_helix[key][j][0]
            end = dic_helix[key][j][1]
            selection = 'resid %i to %i' % (start, end)
            t_helix = t_prot.atom_slice(topology.select(selection))
            print t_helix
            phis = md.compute_phi(t_helix)[1]
            psis = md.compute_psi(t_helix)[1]
            mean_phi = rad_to_deg(mean(psis))
            mean_psi = rad_to_deg(mean(phis))
            if key in dic_phsi:
                dic_phsi[key].append([mean_phi, mean_psi])
            else :
                dic_phsi[key] = [[mean_phi, mean_psi]]
    return dic_phsi

def get_mean_xy(pos):
    #to change to test the real data
    pos = np.array([
                    [[ 4.81400013 , 2.69099998 , 4.91400003],
                     [ 5.03299999 ,3.15199995 , 5.10699987],
                     [ 5.20100021 , 2.8039999  , 5.51100016],
                     [ 5.24499989 , 2.44300008 , 5.09899998]],
                    [[ 4.67       , 2.56       ,      0    ] ,       #frame 2
                     [ 5.04       , 3.5        ,     0     ],
                     [ 5.4        , 3.0        ,    0      ],
                     [ 5.5        , 2.5        ,   0       ]]
                    ])

                    mean_xy =   sum(pos)/len(pos)
                    #only X/Y columns for all residues
    return mean_xy[:,:2]

def get_xy_helix(t_prot, dic_helix):
    dic_pos_xy = {}
    for key in dic_helix.keys():
        for j in range(len(dic_helix[key])):
            start = dic_helix[key][j][0]
            end = dic_helix[key][j][1]
            selection = 'resid %i to %i and name CB' % (start, end)
            t_helix = t_prot.atom_slice(topology.select(selection))
            pos = t_helix.xyz
            mean_xy = get_mean_xy(pos)
            if key not in dic_pos_xy.keys():
                dic_pos_xy[key] = [mean_xy]
            else:
                dic_pos_xy[key].append([mean_xy])
    return dic_pos_xy

def check_11aa_helix(list_xy):
    #Function qui check si les résidues éloignés de 11 ont des X/Y proche
    # i.e detection d'hélice 11-3
    return 1

def ind_2_leaflets(t_POPC):
    pos_POPC = t_POPC.xyz
    P8_mean = pos_POPC[:,:,2].mean()
    #np.where = array[0] = rows, array[1] = columns
    ind_up_POPC =  np.where(pos_POPC[:,:,2] > P8_mean)[1]
    ind_low_POPC =  np.where(pos_POPC[:,:,2] < P8_mean)[1]
    return (ind_up_POPC, ind_low_POPC)

def mean_z_CA_prot(t_CA):
    z = t_protCA.xyz[:,:,2]
    return z.mean()

def mean_z_leaflets_POPC(t_POPC):
    (ind_up_POPC, ind_low_POPC) = ind_2_leaflets(t_POPC)
    up_POPC_z = pos_POPC[:,ind_up_POPC,2]
    mean_up =  up_POPC.mean()
    low_POPC_z = pos_POPC[:,ind_low_POPC,2]
    mean_low =  low_POPC.mean()
    return (mean_up, mean_low)

def dist_prot_lip(t_CA, t_POPC):
    mean_z_prot = mean_z_CA_prot(t_CA)
    (mean_up, mean_low) = mean_z_leaflets_POPC(t_POPC)
    dist_CA_low = np.abs(mean_low - mean_z_prot)
    dist_CA_up = np.abs(mean_up - mean_z_prot)
    return min(dist_CA_low, dist_CA_up)

def dist_helix_lip(t_prot, t_POPC, dic_helix):
    dic_dist_helix_lip = {}
    for key in dic_helix.keys():
        for j in range(len(dic_helix[key])):
            start = dic_helix[key][j][0]
            end = dic_helix[key][j][1]
            print start, end
            selection = 'resid %i to %i and name CA' % (start, end)
            t_helix = t_prot.atom_slice(topology.select(selection))
            dist = dist_prot_lip(t_helix, t_POPC)
            if key not in dic_dist_helix_lip.keys():
                dic_dist_helix_lip[key] = [dist]
            else:
                dic_dist_helix_lip[key].append([dist])
    return dic_dist_helix_lip

pdb = md.load('last.pdb')
res_dssp = md.compute_dssp(pdb)
prot = topology.select("protein and name CA")
t_prot = pdb.atom_slice(topology.select("protein"))
res_dssp = md.compute_dssp(t_prot)

#Test
dssp_test = np.array(['C', 'C', 'H','H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H',
                      'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H',
                      'H', 'H', 'H', 'H', 'H', 'H', 'C'])

dssp_tests = np.vstack((res_dssp[0], dssp_test))
dic_count = compute_structure(dssp_tests)

#Secondary structure and helicity
consensus_struct = get_consensus_structure(dic_count)
helicity = get_helicity(consensus_struct)

#Residu position of the helices and phi/psi mean by helices
helices = get_helices(consensus_struct)
dic_phsi = get_phi_psi(t_prot, helices)

#Attempt to detect 11-mer based on X/Y position superposition
#hyp : if each 11 residues, the X/Y a closed => maybe 11-helix
list_xy = get_xy_helix(t_prot, helices)

#Position of the peptide compared to the bilayer
t_protCA = pdb.atom_slice(topology.select("protein and name CA"))
t_POPC = pdb.atom_slice(topology.select("resname POP and name P8"))
dist_prot_lip(t_protCA, t_POPC)

#Position of each each helix compared to the bilayer (here just one helix)
dist_helix_lip(t_prot, t_POPC, helices)

max_stars_repo_name: Azeann/Test

max_issues_repo_name: Azeann/Test

max_forks_repo_name: Azeann/Test


text:
import argparse
import csv
import json
import os
from statistics import mean
import wandb
import matplotlib
import numpy as np

os.environ['WANDB_API_KEY'] = "7110d81f721ee9a7da84c67bcb319fc902f7a180"
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
api = wandb.Api()

# Set plotting style
sns.set_context('paper', font_scale=1.3)
sns.set_style('whitegrid')
sns.set_palette('colorblind')
plt.rcParams['text.usetex'] = False

def SetPlotRC():
    #If fonttype = 1 doesn't work with LaTeX, try fonttype 42.
    plt.rc('pdf',fonttype = 42)
    plt.rc('ps',fonttype = 42)

def ApplyFont(ax):

    ticks = ax.get_xticklabels() + ax.get_yticklabels()

    text_size = 14.0

    for t in ticks:
        t.set_fontname('Times New Roman')
        t.set_fontsize(text_size)

    txt = ax.get_xlabel()
    txt_obj = ax.set_xlabel(txt)
    txt_obj.set_fontname('Times New Roman')
    txt_obj.set_fontsize(text_size)

    txt = ax.get_ylabel()
    txt_obj = ax.set_ylabe(txt)
    txt_obj.set_fontname('Times New Roman')
    txt_obj.set_fontsize(text_size)

    txt = ax.get_title()
    txt_obj = ax.set_title(txt)
    txt_obj.set_fontname('Times New Roman')
    txt_obj.set_fontsize(text_size)

SetPlotRC()

def data_to_extract(username,args):
    labels = {}
    labels['title'] = "PPI Link Prediction"
    labels['x_label'] = "Gradient Updates"
    labels['y_label'] = "Percent"
    if args.local:
        param_str = 'Local'
    else:
        param_str = 'Global'

    labels['train_metric_auc'] = "Train_" + param_str + "_Graph_"
    labels['train_metric_ap'] = "Train_" + param_str + "_Graph_"
    labels['test_metric_auc'] = "Test_" + param_str + "_Graph_"
    labels['test_metric_ap'] = "Test_" + param_str + "_Graph_"
    if username == "joeybose":
      labels['experiments_key'] = [[args.one_maml],\
                                   [args.two_maml],\
                                   [args.random_exp],\
                                   [args.no_finetune],\
                                   [args.finetune],\
                                   [args.adamic_adar],\
                                   [args.graph_sig],\
                                   ]
    if args.local:
        labels['experiments_name'] = ['1-MAML','2-MAML', 'NoFinetune',\
                  'Finetune','Adamic-Adar']
    else:
        labels['experiments_name'] = ['1-MAML','2-MAML', 'Random', 'NoFinetune',\
              'Finetune']

    return labels

def data_to_extract_ppi(username,args):
    labels = {}
    labels['title'] = "PPI Link Prediction"
    labels['x_label'] = "Gradient Updates"
    labels['y_label'] = "Percent"
    if args.local:
        param_str = 'Local'
    else:
        param_str = 'Global'

    labels['train_metric_auc'] = "Train_" + param_str + "_Graph_"
    labels['train_metric_ap'] = "Train_" + param_str + "_Graph_"
    labels['test_metric_auc'] = "Test_" + param_str + "_Graph_"
    labels['test_metric_ap'] = "Test_" + param_str + "_Graph_"
    if username == "joeybose":
      labels['experiments_key'] = [[args.two_maml],\
                                   [args.concat],\
                                   [args.random_exp],\
                                   [args.no_finetune],\
                                   [args.finetune],\
                                   [args.adamic_adar],\
                                   [args.mlp],\
                                   [args.graph_sig],\
                                   [args.graph_sig_concat],\
                                   [args.graph_sig_sig],\
                                   [args.graph_sig_weights],\
                                   [args.graph_sig_random]\
                                   ]

    labels['experiments_name'] = ['2-MAML', 'MAML','Random', 'NoFinetune',\
          'Finetune', 'Adamic-Adar', 'MLP', 'Graph-Sig', 'Graph-Sig-Concat',\
                                  'GS-Gating','GS-Weights', 'Graph-Sig-Random']

    if args.local:
        if args.local_block is not None:
            for block in sorted(args.local_block, reverse=True):
                del labels['experiments_name'][block]
                del labels['experiments_key'][block]
    else:
        if args.global_block is not None:
            for block in sorted(args.global_block, reverse=True):
                del labels['experiments_name'][block]
                del labels['experiments_key'][block]
    return labels

def data_to_extract_firstmmdb(username,args):
    labels = {}
    labels['title'] = "FIRSTMMDB Link Prediction"
    labels['x_label'] = "Gradient Updates"
    labels['y_label'] = "AUC/AP"
    if args.local:
        param_str = 'Local'
    else:
        param_str = 'Global'

    labels['train_metric_auc'] = "Train_" + param_str + "_Graph_"
    labels['train_metric_ap'] = "Train_" + param_str + "_Graph_"
    labels['test_metric_auc'] = "Test_" + param_str + "_Graph_"
    labels['test_metric_ap'] = "Test_" + param_str + "_Graph_"
    if username == "joeybose":
      labels['experiments_key'] = [[args.two_maml],\
                                   [args.concat],\
                                   [args.random_exp],\
                                   [args.no_finetune],\
                                   [args.finetune],\
                                   [args.adamic_adar],\
                                   [args.mlp],\
                                   [args.graph_sig],\
                                   [args.graph_sig_sig],\
                                   [args.graph_sig_weights],\
                                   [args.graph_sig_concat],\
                                   [args.graph_sig_random]\
                                   ]

    labels['experiments_name'] = ['MAML', 'MAML-Concat','Random', 'NoFinetune',\
          'Finetune', 'Adamic-Adar', 'MLP', 'Graph-Sig', 'GS-Gating',\
                                  'GS-Weights','Graph-Sig Concat','Graph-Sig-Random']

    if args.local:
        if args.local_block is not None:
            for block in sorted(args.local_block, reverse=True):
                del labels['experiments_name'][block]
                del labels['experiments_key'][block]
    else:
        if args.global_block is not None:
            for block in sorted(args.global_block, reverse=True):
                del labels['experiments_name'][block]
                del labels['experiments_key'][block]
    return labels

def data_to_extract_reddit(username,args):
    labels = {}
    labels['title'] = "Reddit Link Prediction"
    labels['x_label'] = "Gradient Updates"
    labels['y_label'] = "Percent"
    if args.local:
        param_str = 'Local_Batch'
    else:
        param_str = 'Global_Batch'

    labels['train_metric_auc'] = "Train_" + param_str + "_Graph_"
    labels['train_metric_ap'] = "Train_" + param_str + "_Graph_"
    labels['test_metric_auc'] = "Test_" + param_str + "_Graph_"
    labels['test_metric_ap'] = "Test_" + param_str + "_Graph_"
    if username == "joeybose":
      labels['experiments_key'] = [[args.two_maml],\
                                   [args.random_exp],\
                                   [args.no_finetune],\
                                   [args.finetune],\
                                   [args.adamic_adar],\
                                   [args.mlp],\
                                   [args.graph_sig]\
                                   ]
    labels['experiments_name'] = ['2-MAML', 'Random', 'NoFinetune',\
          'Finetune', 'Adamic-Adar', 'MLP', 'Graph-Sig']

    if args.local:
        for block in sorted(args.local_block, reverse=True):
            del labels['experiments_name'][block]
            del labels['experiments_key'][block]
    else:
        for block in sorted(args.global_block, reverse=True):
            del labels['experiments_name'][block]
            del labels['experiments_key'][block]

    return labels

def truncate_exp(data_experiments):
    last_data_points = [run[-1] for data_run in data_experiments for run in data_run]
    run_end_times = [timestep for timestep, value in last_data_points]
    earliest_end_time = min(run_end_times)

    clean_data_experiments = []
    for exp in data_experiments:
        clean_data_runs = []
        for run in exp:
            clean_data_runs.append({x: y for x, y in run if x <= earliest_end_time})
        clean_data_experiments.append(clean_data_runs)

    return clean_data_experiments

def truncate_array(experiment_list):
    min_len = 10000000
    for exp in experiment_list:
        length = exp.shape[1]
        if length < min_len:
            min_len = length
    trunc_exp_list = []
    for exp in experiment_list:
        trunc_exp = np.vstack((exp[0][:min_len],exp[1][:min_len])).T
        trunc_exp_list.append(trunc_exp)
    return trunc_exp_list

def get_grad_step_data(args, labels, wandb_username, wandb_project):
    # Accumulate data for all experiments.
    data_experiments_auc = []
    data_experiments_ap = []
    for runs, label in zip(labels.get('experiments_key'), labels.get('experiments_name')):
        if len(runs[0]) > 0:
            for exp_key in runs:
                try:
                    my_run = api.run("%s/%s/%s" %(wandb_username,\
                            wandb_project,exp_key))
                except:
                    temp_wandb_project='meta-graph'
                    my_run = api.run("%s/%s/%s" %(wandb_username,\
                            temp_wandb_project,exp_key))
                raw_data = my_run.history(samples=1000000)
                keys = raw_data.keys().values
                test_inner_avg_auc = 'Test_Complete_AUC'
                test_inner_avg_ap = 'Test_Complete_AP'
                test_avg_auc = 'Test_Avg__AUC'
                test_avg_ap = 'Test_Avg__AP'
                try:
                    data_points_auc = raw_data[test_inner_avg_auc].dropna().values
                    data_points_ap = raw_data[test_inner_avg_ap].dropna().values
                    data_points_auc_array = [[i,point] for i, point in enumerate(data_points_auc)]
                    data_points_ap_array = [[i,point] for i, point in enumerate(data_points_ap)]
                    data_experiments_auc.append(np.asarray(data_points_auc_array).T)
                    data_experiments_ap.append(np.asarray(data_points_ap_array).T)
                except:
                    data_points_auc = raw_data[test_avg_auc].dropna().values
                    data_points_ap = raw_data[test_avg_ap].dropna().values
                    data_points_auc_array = [[i,point] for i, point in enumerate(data_points_auc)]
                    data_points_ap_array = [[i,point] for i, point in enumerate(data_points_ap)]
                    data_experiments_auc.append(np.asarray(data_points_auc_array).T)
                    data_experiments_ap.append(np.asarray(data_points_ap_array).T)

            print("%s AUC %f After %d Grad Steps"%(my_run.name,data_points_auc[int(args.get_step[0]/5)], args.get_step[0]))
            print("%s AP %f After %d Grad Steps"%(my_run.name,data_points_ap[int(args.get_step[0]/5)], args.get_step[0]))
            print("Max AUC %f"%(data_points_auc.max()))
            print("Max AP %f"%(data_points_ap.max()))
    clean_data_experiments_auc = truncate_array(data_experiments_auc)
    clean_data_experiments_ap = truncate_array(data_experiments_ap)
    return clean_data_experiments_auc, clean_data_experiments_ap

def get_data(args, title, x_label, y_label, labels_list, data, wandb_username, wandb_project):
    if not title or not x_label or not y_label or not labels_list:
        print("Error!!! Ensure filename, x and y labels,\
        and metric are present.")
        exit(1)

    train_auc = labels_list['train_metric_auc']
    train_ap = labels_list['train_metric_ap']
    test_auc = labels_list['train_metric_auc']
    test_ap = labels_list['train_metric_ap']

    # Accumulate data for all experiments.
    data_experiments_auc = []
    data_experiments_ap = []
    for i, runs in enumerate(data):
        # Accumulate data for all runs of a given experiment.
        data_runs_auc = []
        data_runs_ap = []
        if len(runs) > 0:
            for exp_key in runs:
                try:
                    my_run = api.run("%s/%s/%s" %(wandb_username,\
                            wandb_project,exp_key))
                except:
                    wandb_project='meta-graph-ppi'
                    my_run = api.run("%s/%s/%s" %(wandb_username,\
                            wandb_project,exp_key))
                raw_data = my_run.history(samples=100000)
                keys = raw_data.keys().values
                train_keys = [key for key in keys if 'Train' in key]
                test_keys = [key for key in keys if 'Test' in key]
                train_local_keys = [key for key in train_keys if 'Local' in key]
                train_global_keys = [key for key in train_keys if 'Local' not in key]
                train_global_keys = [key for key in train_global_keys if 'Complete' not in key]
                train_global_keys = [key for key in train_global_keys if 'Avg' not in key]
                test_local_keys = [key for key in test_keys if 'Local' in key]
                test_global_keys = [key for key in test_keys if 'Local' not in key]
                test_global_keys = [key for key in test_global_keys if 'Complete' not in key]
                test_global_keys = [key for key in test_global_keys if 'Avg' not in key]

                ''' Add Random back in '''
                if (args.dataset == 'PPI' or args.dataset =='FIRSTMM_DB')  and i == 2 and args.local:
                    train_local_keys = train_global_keys
                    test_local_keys = test_global_keys
                elif (args.dataset == 'PPI' or args.dataset=='FIRSTMM_DB') and i == 9 and args.local:
                    train_local_keys = train_global_keys
                    test_local_keys = test_global_keys

                if args.mode == 'Train':
                    if args.local:
                        for key in train_local_keys:
                            if 'AUC' in key:
                                data_points_auc = raw_data[key].dropna().values
                                data_points_auc = [[i+1,point] for i, point in enumerate(data_points_auc)]
                                data_runs_auc.append(data_points_auc)
                            else:
                                data_points_ap = raw_data[key].dropna().values
                                data_points_ap = [[i+1,point] for i, point in enumerate(data_points_ap)]
                                data_runs_ap.append(data_points_ap)
                    else:
                        for key in train_global_keys:
                            if 'AUC' in key:
                                data_points_auc = raw_data[key].dropna().values
                                data_points_auc = [[i+1,point] for i, point in enumerate(data_points_auc)]
                                data_runs_auc.append(data_points_auc)
                            else:
                                data_points_ap = raw_data[key].dropna().values
                                data_points_ap = [[i+1,point] for i, point in enumerate(data_points_ap)]
                                data_runs_ap.append(data_points_ap)
                else:
                    if args.local:
                        for key in test_local_keys:
                            if 'AUC' in key:
                                data_points_auc = raw_data[key].dropna().values
                                data_points_auc = [[i,point] for i, point in enumerate(data_points_auc)]
                                data_runs_auc.append(data_points_auc)
                            else:
                                data_points_ap = raw_data[key].dropna().values
                                data_points_ap = [[i,point] for i, point in enumerate(data_points_ap)]
                                data_runs_ap.append(data_points_ap)
                    else:
                        for key in test_global_keys:
                            if 'AUC' in key:
                                data_points_auc = raw_data[key].dropna().values
                                data_points_auc = [[i,point] for i, point in enumerate(data_points_auc)]
                                data_runs_auc.append(data_points_auc)
                            else:
                                data_points_ap = raw_data[key].dropna().values
                                data_points_ap = [[i,point] for i, point in enumerate(data_points_ap)]
                                data_runs_ap.append(data_points_ap)
            data_experiments_auc.append(data_runs_auc)
            data_experiments_ap.append(data_runs_ap)
    clean_data_experiments_auc = truncate_exp(data_experiments_auc)
    clean_data_experiments_ap = truncate_exp(data_experiments_ap)
    return clean_data_experiments_auc, clean_data_experiments_ap

def my_plot(**kwargs):
    labels = kwargs.get('labels')
    data = kwargs.get('data')

    # Setup figure
    fig = plt.figure(figsize=(12, 8))
    ax = plt.subplot()

    for label in (ax.get_xticklabels()):
        label.set_fontname('Arial')
        label.set_fontsize(20)
    for label in (ax.get_yticklabels()):
        label.set_fontname('Arial')
        label.set_fontsize(20)
    plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
    # plt.yticks(np.arange(0.5, 1, 0.05))
    ax.set_yticks(np.arange(0.5, 1, 0.05))
    # plt.xticks(np.arange(0, 200, 5))
    ax.xaxis.get_offset_text().set_fontsize(5)
    ax.yaxis.get_offset_text().set_fontsize(5)
    axis_font = {'fontname': 'Arial', 'size': '24'}
    colors = sns.color_palette('colorblind', n_colors=len(data))

    # Plot data
    if args.mode=='Test':
        position = 0

    my_labels = []
    for label,key in zip(labels.get('experiments_name'),labels.get('experiments_key')):
        if len(key[0]) > 0:
            my_labels.append(label)

    for runs, label, color in zip(data, my_labels, colors):
        unique_x_values = set()

        # Plot mean and standard deviation of all runs
        y_values_mean = []
        y_values_std = []
        runs = np.asarray(runs).T
        x_values = runs[0]
        y_values = runs[1]
        # Plot mean
        plt.plot(x_values, y_values, color=color, linewidth=2.5, label=label)

    # Label figure
    ax.legend(loc='lower right', prop={'size': 16})
    if args.mode == 'Train':
        ax.set_xlabel(labels.get('x_label'), **axis_font)
    ax.set_ylabel(labels.get('y_label'), **axis_font)
    fig.subplots_adjust(bottom=0.2)
    fig.subplots_adjust(left=0.2)

    # remove grid lines
    ax.grid(False)
    plt.grid(b=False, color='w')
    return fig

def plot(**kwargs):
    labels = kwargs.get('labels')
    data = kwargs.get('data')

    # Setup figure
    fig = plt.figure(figsize=(12, 8))
    ax = plt.subplot()

    for label in (ax.get_xticklabels()):
        label.set_fontname('Arial')
        label.set_fontsize(20)
    for label in (ax.get_yticklabels()):
        label.set_fontname('Arial')
        label.set_fontsize(20)
    plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
    plt.yticks(np.arange(0, 1, 0.1))
    ax.xaxis.get_offset_text().set_fontsize(10)
    axis_font = {'fontname': 'Arial', 'size': '24'}
    colors = sns.color_palette('colorblind', n_colors=len(data))

    # Plot data
    if args.mode=='Test':
        position = 0

    for runs, label, color in zip(data, labels.get('experiments_name'), colors):
        unique_x_values = set()
        for run in runs:
            for key in run.keys():
                unique_x_values.add(key)
        x_values = sorted(unique_x_values)

        # Plot mean and standard deviation of all runs
        y_values_mean = []
        y_values_std = []
        for x in x_values:
            y_values_mean.append(mean([run.get(x) for run in runs if run.get(x)]))
            y_values_std.append(np.std([run.get(x) for run in runs if run.get(x)]))

        print("%s average result after graphs %f" %(label,y_values_mean[-1]))
        if args.mode =='Train' or args.no_bar_plot:
            x_values.insert(0,0)
            y_values_mean.insert(0,0)
            y_values_std.insert(0,0)
            # Plot std
            ax.fill_between(x_values, np.add(np.array(y_values_mean), np.array(y_values_std)),
                            np.subtract(np.array(y_values_mean), np.array(y_values_std)),
                            alpha=0.3,
                            edgecolor=color, facecolor=color)
            # Plot mean
            plt.plot(x_values, y_values_mean, color=color, linewidth=1.5, label=label)
        else:
            plt.bar(position, y_values_mean, width=0.5, color=color,\
                    label=label, yerr=y_values_std, error_kw=dict(lw=5, capsize=25, capthick=2))
            position += 1

    # Label figure
    ax.legend(loc='lower right', prop={'size': 16})
    if args.mode == 'Train':
        ax.set_xlabel(labels.get('x_label'), **axis_font)
    ax.set_ylabel(labels.get('y_label'), **axis_font)
    fig.subplots_adjust(bottom=0.2)
    fig.subplots_adjust(left=0.2)

    # remove grid lines
    ax.grid(False)
    plt.grid(b=False, color='w')
    return fig

def main(args):
    wandb_project = "meta-graph"
    wandb_username = "joey-bose"

    if args.dataset == 'PPI':
        wandb_project = "meta-graph-ppi"
        extract_func = data_to_extract_ppi
    elif args.dataset == 'Reddit':
        wandb_project = "meta-graph-reddit"
        extract_func = data_to_extract_reddit
    elif args.dataset == 'FIRSTMM_DB':
        wandb_project = "meta-graph-firstmmdb"
        extract_func = data_to_extract_firstmmdb
    elif args.dataset=='AMINER':
        wandb_project = "meta-graph-aminer"
        extract_func = data_to_extract_firstmmdb

    labels = extract_func("joeybose",args)
    if args.get_grad_steps:
        data_experiments_auc, data_experiments_ap = get_grad_step_data(args,labels=labels,\
                wandb_project=wandb_project,wandb_username=wandb_username)
        labels['y_label'] = 'AUC'
        fig_auc = my_plot(labels=labels, data=data_experiments_auc)
        labels['y_label'] = 'AP'
        fig_ap = my_plot(labels=labels, data=data_experiments_ap)
        plot_dir = '../plots_datasets/'+ args.dataset + '/'
        if not os.path.exists(plot_dir):
            os.makedirs(plot_dir)
        fig_auc.savefig('../plots_datasets/'+ args.dataset + '/' + \
                        args.name_str+ '_'+ args.file_str + '_' + args.mode +'_grad_AUC.png')
        fig_ap.savefig('../plots_datasets/'+ args.dataset + '/' + args.name_str +\
                       '_' + args.file_str + '_' + args.mode + '_grad_AP.png')
        print("Finished plotting AUC + AP")
    else:
        data_experiments_auc, data_experiments_ap = get_data(args,labels.get('title'), labels.get('x_label'),\
                                    labels.get('y_label'), labels,\
                                    labels.get('experiments_key'),\
                                    wandb_project=wandb_project,\
                                    wandb_username=wandb_username)
        fig_auc = plot(labels=labels, data=data_experiments_auc)
        fig_ap = plot(labels=labels, data=data_experiments_ap)
        if args.local:
            param_str = '_Local_'
        else:
            param_str = '_Global_'

        plot_dir = '../plots_datasets/'+ args.dataset + '/'
        if not os.path.exists(plot_dir):
            os.makedirs(plot_dir)

        fig_auc.savefig('../plots_datasets/'+ args.dataset + '/' + args.file_str + param_str +'_AUC.png')
        fig_ap.savefig('../plots_datasets/'+ args.dataset + '/' + args.file_str + param_str + '_AP.png')
        print("Finished plotting AUC + AP")

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--source_filename', default='plot_source.csv')
    parser.add_argument("--local", action="store_true", default=False)
    parser.add_argument("--no_bar_plot", action="store_true", default=False)
    parser.add_argument("--get_grad_steps", action="store_true", default=False)
    parser.add_argument('--mode', type=str, default='Train')
    parser.add_argument('--file_str', type=str, default='')
    parser.add_argument('--name_str', type=str, default='')
    parser.add_argument('--one_maml', type=str, default='')
    parser.add_argument('--two_maml', type=str, default='')
    parser.add_argument('--concat', type=str, default='')
    parser.add_argument('--random_exp', type=str, default='')
    parser.add_argument('--no_finetune', type=str, default='')
    parser.add_argument('--finetune', type=str, default='')
    parser.add_argument('--adamic_adar', type=str, default='')
    parser.add_argument('--mlp', type=str, default='')
    parser.add_argument('--graph_sig', type=str, default='')
    parser.add_argument('--graph_sig_sig', type=str, default='')
    parser.add_argument('--graph_sig_weights', type=str, default='')
    parser.add_argument('--graph_sig_concat', type=str, default='')
    parser.add_argument('--graph_sig_random', type=str, default='')
    parser.add_argument('--dataset', type=str, default='PPI')
    parser.add_argument('--local_block', nargs='+', type=int)
    parser.add_argument('--global_block', nargs='+', type=int)
    parser.add_argument('--get_step', nargs='+', default=5, type=int)
    args = parser.parse_args()

    main(args)

max_stars_repo_name: joeybose/vgraphgen

max_issues_repo_name: joeybose/vgraphgen

max_forks_repo_name: joeybose/vgraphgen


text:
# -*- coding: utf-8 -*-

# Basic integrity test to check is AxonDeepSeg is correctly installed.
# Launches a segmentation in the data_test folder.

import json
from pathlib import Path
import numpy as np

# AxonDeepSeg imports
from AxonDeepSeg.testing.segmentation_scoring import pw_dice
from AxonDeepSeg.apply_model import axon_segmentation
import AxonDeepSeg.ads_utils as ads
import AxonDeepSeg.ads_utils
from config import axonmyelin_suffix

def integrity_test():

    try:

        # get path of directory where AxonDeepSeg was installed
        dir_path = Path(__file__).resolve().parent

        # input parameters

        path = Path('folder_name') / 'file_name'
        model_name = 'model_seg_rat_axon-myelin_sem'
        path_model = dir_path / 'models' / model_name
        path_testing = path_model / 'data_test'
        image = Path("image.png")

        # Launch the axon and myelin segmentation on test image sample provided in the installation
        print('Computing the segmentation of axon and myelin on test image.')
        axon_segmentation(path_testing, [str(path_testing / image)], path_model, acquired_resolution=0.13, overlap_value=[48,48])

        # Read the ground truth mask and the obtained segmentation mask
        mask = ads.imread(path_testing / 'mask.png')
        pred = ads.imread(path_testing / (image.stem + str(axonmyelin_suffix)))

        # Generate separate axon and myelin masks of the segmentation output
        print('Generating axon and myelin segmentation masks and saving.')
        gt_axon = mask > 200 # Generate binary image with the axons for the ground truth (myelin=127, axon=255)
        gt_myelin = np.logical_and(mask >= 50, mask <= 200) # Generate binary image with the myelin for the ground truth (myelin=127, axon=255)

        pred_axon = pred > 200 # Generate binary image with the axons for the segmentation (myelin=127, axon=255)
        pred_myelin = np.logical_and(pred >= 50, pred <= 200) # Generate binary image with the myelin for the segmentation (myelin=127, axon=255)

        # Compute Dice between segmentation and ground truth, for both axon and myelin
        dice_axon = pw_dice(pred_axon, gt_axon)
        dice_myelin = pw_dice(pred_myelin, gt_myelin)

        # If all the commands above are executed without bugs, the installation is done correctly
        print("* * * Integrity test passed. AxonDeepSeg is correctly installed. * * * ")
        return 0

    except IOError:

        # Else, there is a problem in the installation
        print("Integrity test failed... ")
        return -1

max_stars_repo_name: axondeepseg/axondeepseg

max_issues_repo_name: axondeepseg/axondeepseg

max_forks_repo_name: axondeepseg/axondeepseg


text:
import sys
sys.path.append("models")
sys.path.append(r"models/research")

import tensorflow as tf
import numpy as np
import cv2

from object_detection.utils import label_map_util
from object_detection.utils import config_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder

def detect_fn(image, detection_model):
    image, shapes = detection_model.preprocess(image)
    prediction_dict = detection_model.predict(image, shapes)
    detections = detection_model.postprocess(prediction_dict, shapes)

    return detections, prediction_dict, tf.reshape(shapes, [-1])

def run(label_map_path, config_file_path, checkpoint_path):

    # Enable GPU dynamic memory allocation
    gpus = tf.config.experimental.list_physical_devices('GPU')
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)

    # Load pipeline config and build a detection model
    configs = config_util.get_configs_from_pipeline_file(config_file_path)
    model_config = configs['model']
    detection_model = model_builder.build(model_config=model_config, is_training=False)

    # Restore checkpoint
    ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
    ckpt.restore(checkpoint_path).expect_partial()

    category_index = label_map_util.create_category_index_from_labelmap(label_map_path, use_display_name=True)

    cap = cv2.VideoCapture(0)
    while True:
        # Read frame from camera
        ret, image_np = cap.read()

        input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
        detections, predictions_dict, shapes = detect_fn(input_tensor, detection_model)

        detection_boxes = detections['detection_boxes'][0].numpy()
        detection_classes = detections['detection_classes'][0].numpy()
        detection_scores = detections['detection_scores'][0].numpy()

        indexes = np.array(tf.image.non_max_suppression(
            detection_boxes,
            detection_scores,
            max_output_size=100,
            iou_threshold=0.5,
            score_threshold=0.3))

        detection_boxes = detection_boxes[indexes]
        detection_classes = detection_classes[indexes]
        detection_scores = detection_scores[indexes]

        label_id_offset = 1
        image_np_with_detections = image_np.copy()

        if indexes.shape != 0:
            viz_utils.visualize_boxes_and_labels_on_image_array(
                image_np_with_detections,
                detection_boxes,
                (detection_classes + label_id_offset).astype(int),
                detection_scores,
                category_index,
                use_normalized_coordinates=True,
                max_boxes_to_draw=10,
                min_score_thresh=.30,
                agnostic_mode=False)

        # Display output
        cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))

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

    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    PATH_TO_LABELMAP = r"model/label_map.pbtxt"
    PATH_TO_CONFIG = r"model/pipeline.config"
    PATH_TO_CHECKPOINT = r"model/checkpoint/ckpt-18"

    run(label_map_path=PATH_TO_LABELMAP, config_file_path=PATH_TO_CONFIG, checkpoint_path=PATH_TO_CHECKPOINT)


text:
import numpy as np
from .catch_errors import check_for_period_error
from .function_helper import fill_for_noncomputable_vals

np.seterr(divide='ignore', invalid='ignore')

def standard_variance(data, period):
    """
    Standard Variance.

    Formula:
    (Ct - AVGt)^2 / N
    """
    check_for_period_error(data, period)
    sv = list(map(
        lambda idx:
        np.var(data[idx+1-period:idx+1], ddof=1),
        range(period-1, len(data))
        ))
    sv = fill_for_noncomputable_vals(data, sv)

    return sv


text:
"""Tests asdf implementations of python base types."""
import uuid

import numpy as np
import pytest
import xarray as xr
from asdf import ValidationError

from weldx.asdf.util import write_read_buffer

# --------------------------------------------------------------------------------------
# uuid
# --------------------------------------------------------------------------------------
def test_uuid():
    """Test uuid serialization and version 4 pattern."""
    write_read_buffer({"id": uuid.uuid4()})

    with pytest.raises(ValidationError):
        write_read_buffer({"id": uuid.uuid1()})

# --------------------------------------------------------------------------------------
# xarray
# --------------------------------------------------------------------------------------
def test_dataarray():
    da = xr.DataArray(np.random.randn(2, 3), dims=("x", "y"), coords={"x": [10, 20]})
    da.attrs["long_name"] = "random velocity"
    # add metadata to coordinate
    da.x.attrs["units"] = "x units"
    da2 = write_read_buffer({"da": da})["da"]
    assert da2.identical(da)

def test_dataset_children():
    da = xr.DataArray(np.arange(10), name="arr1", attrs={"name": "sample data"})
    ds = da.to_dataset()
    ds2 = write_read_buffer({"ds": ds})["ds"]
    assert ds2.identical(ds)

max_stars_repo_name: CagtayFabry/weldx

max_issues_repo_name: BAMWelDX/weldx

max_forks_repo_name: CagtayFabry/weldx


text:
import random

from yolor.utils.general import clip_coords

import numpy as np
import torch

def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
    # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw  # top left x
    y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh  # top left y
    y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw  # bottom right x
    y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh  # bottom right y
    return y

def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0):
    # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right
    if clip:
        clip_coords(x, (h - eps, w - eps))  # warning: inplace clip
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w  # x center
    y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h  # y center
    y[:, 2] = (x[:, 2] - x[:, 0]) / w  # width
    y[:, 3] = (x[:, 3] - x[:, 1]) / h  # height
    return y

# takes  n1 x 4, n2 x 4 sets of boxes and returns an n1 x n2 array of pair-wise intersection-over-area scores
def IoA(a, b):
    a = a[:,:,None]
    if isinstance(a, np.ndarray) and isinstance(b, np.ndarray):
        x_overlap = np.maximum(0, np.minimum(a[:,2], b[:,2])- np.maximum(a[:,0], b[:,0]))
        y_overlap = np.maximum(0, np.minimum(a[:,3], b[:,3])- np.maximum(a[:,1], b[:,1]))
        return x_overlap*y_overlap/(np.abs(a[:,2] - a[:,0])*np.abs(a[:,3] - a[:,1]))
    elif isinstance(a, torch.Tensor) and isinstance(b, torch.Tensor):
        z = torch.tensor(0, device=a.device)
        x_overlap = torch.maximum(z, torch.minimum(a[:,2], b[:,2])- torch.maximum(a[:,0], b[:,0]))
        y_overlap = torch.maximum(z, torch.minimum(a[:,3], b[:,3])- torch.maximum(a[:,1], b[:,1]))
        return x_overlap*y_overlap/(torch.abs(a[:,2] - a[:,0])*torch.abs(a[:,3] - a[:,1]))
    else:
        raise Exception('both inputs must be of type np.ndarray or torch.Tensor, simultaneously')
def box_ioa(box1, box2):

    def box_area(box):
        # box = 4xn
        return (box[2] - box[0]) * (box[3] - box[1])

    area1 = box_area(box1.T)
    #area2 = box_area(box2.T)

    # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
    inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2)
    return inter / (area1[:, None])

#random.seed(12342343456)
def mask_augment(imgs, targets):
    B, _, H, W = imgs.size()

    targets[:,-4:] = xywhn2xyxy(targets[:,-4:], w=W, h=H)

    retain_mask = torch.ones((len(targets)),dtype=torch.bool,device=targets.device)
    for i in range(B):
        if random.randint(1,10) > 3:
            r = random.randint(0, 3)
            ratio = 0.2
            x = random.randint(int(W*ratio), int(W*(1-ratio)))
            y = random.randint(int(H*ratio), int(H*(1-ratio)))

            if r == 0:
                crop = torch.tensor([0, 0, x, H-1])
            elif r == 1:
                crop = torch.tensor([x, 0, W-1, H-1])
            elif r == 2:
                crop = torch.tensor([0, 0, W-1, y])
            elif r == 3:
                crop = torch.tensor([0, y, W-1, H-1])
            crop = crop.to(targets.device)

            # calculate which boxes are still visible within the crop
            intersections = torch.squeeze(box_ioa(targets[targets[:,0] == i, -4:],crop.unsqueeze(0)),dim=1)
            retain_mask[targets[:,0] == i] = intersections > 0.2

            # mask image
            mask = torch.zeros((1,1,H,W), device=imgs.device)
            x1, y1, x2, y2 = crop
            mask[...,y1:y2, x1:x2] = 1
            imgs[i] =mask* imgs[i]

            # crop boxes
            diff = torch.cat([crop[:2], crop[:2]])
            crop_boxes = torch.sub(targets[targets[:,0] == i, -4:], diff)
            clip_coords(crop_boxes, (crop[3]-crop[1], crop[2]-crop[0]))
            crop_boxes = torch.add(crop_boxes, diff)
            targets[targets[:,0] == i, -4:] = crop_boxes
        #else:

    targets[:,-4:] = xyxy2xywhn(targets[:,-4:], w=W, h=H)
    #return imgs, targets[torch.cat(retain_mask)]
    return imgs, targets[retain_mask]

max_stars_repo_name: inspire-group/ObjectSeeker

max_issues_repo_name: inspire-group/ObjectSeeker

max_forks_repo_name: inspire-group/ObjectSeeker


text:
import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
FALSE = tf.Variable(initial_value=False, trainable=False)
TRUE = tf.Variable(initial_value=True, trainable=False)
class Trainer:

    def __init__(self, loss, predictions, optimizer, ds_train, ds_validation, stop_patience, evaluation, inputs, labels):
        '''
            Initialize the trainer

            Args:
                loss        	an operation that computes the loss
                predictions     an operation that computes the predictions for the current
                optimizer       optimizer to use
                ds_train        instance of Dataset that holds the training data
                ds_validation   instance of Dataset that holds the validation data
                stop_patience   the training stops if the validation loss does not decrease for this number of epochs
                evaluation      instance of Evaluation
                inputs          placeholder for model inputs
                labels          placeholder for model labels
        '''
        self._train_op = optimizer.minimize(loss)

        self._loss = loss
        self._predictions = predictions
        self._ds_train = ds_train
        self._ds_validation = ds_validation
        self._stop_patience = stop_patience
        self._evaluation = evaluation
        self._validation_losses = []
        self._model_inputs = inputs
        self._model_labels = labels
        self._train_loss = []
        self._validation_f1 = []

        with tf.variable_scope('model', reuse = True):
            self._model_is_training = tf.get_variable('is_training', dtype = tf.bool, trainable=False)

    def _train_epoch(self, sess):
        '''
            trains for one epoch and prints the mean training loss to the commandline

            args:
                sess    the tensorflow session that should be used
        '''

        # TODO
        self._model_is_training = tf.identity(TRUE, name="is_training")
        dsIter = self._ds_train.__iter__()
        mean_loss = 0
        for i in range(dsIter._len):
            images,labels = next(dsIter)
            _,loss_value,prediction = sess.run([self._train_op,self._loss,self._predictions],feed_dict={self._model_inputs:images,self._model_labels:labels})
            mean_loss = mean_loss + loss_value
            self._evaluation.add_batch(prediction,labels)

        mean_loss = mean_loss/dsIter._len
        self._train_loss.append(mean_loss)
        print("training epoch:")
        self._evaluation.flush()
        print("mean loss: ",mean_loss)

        pass

    def _valid_step(self, sess):
        '''
            run the validation and print evalution + mean validation loss to the commandline

            args:
                sess    the tensorflow session that should be used
        '''

        # TODO
        self._model_is_training = tf.identity(FALSE, name="is_validating")
        dsIter = self._ds_validation.__iter__()
        mean_loss = 0

        for i in range(dsIter._len):
            imgs,labels = next(dsIter)
            prediction,loss_value = sess.run([self._predictions,self._loss],feed_dict={self._model_inputs:imgs,self._model_labels:labels})

            mean_loss = mean_loss + loss_value
            self._evaluation.add_batch(prediction,labels)

        mean_loss = mean_loss / dsIter._len
        self._validation_losses.append(mean_loss)
        f1values = self._evaluation._measures[-1].values()
        f1mean = (f1values[0]+f1values[1]) / 2
        self._validation_f1.append(f1mean)
        print("validation step:")
        self._evaluation.flush()
        print("validation_loss: ",mean_loss)
        print("F1mean: ",f1mean)

        pass

    def _should_stop(self):
        '''
            determine if training should stop according to stop_patience
        '''

        # TODO
        # if len(self._validation_losses)<self._stop_patience:
        #     return False
        # else:
        #     query = self._validation_losses[-self._stop_patience:]
        #     if any(query[i+1] < query[i] for i in range(0,len(query)-1)):
        #         return False
        #     else:
        #         return True
        if len(self._validation_f1)<self._stop_patience:
            return False
        else:
            # if self._validation_f1[-1] > 0.8:
            #     return True
            query = self._validation_f1[-self._stop_patience:]
            if any(query[i+1] > query[i] for i in range(0,len(query)-1)):
                return False
            else:
                return True

        pass

    def run(self, sess, num_epochs=-1):
        '''
            run the training until num_epochs exceeds or the validation loss did not decrease
            for stop_patience epochs

            args:
                sess        the tensorflow session that should be used
                num_epochs  limit to the number of epochs, -1 means not limit
        '''

        # initial validation step
        self._valid_step(sess)

        i = 0

        # training loop
        while i < num_epochs or num_epochs == -1:
            print("epochs:{:d}".format(i))
            self._train_epoch(sess)
            self._valid_step(sess)
            i += 1
            # if i % 30 == 0:
            #     plot = np.round(self._train_loss.copy(), decimals=5)
            #     plt.plot(plot[1:], color="blue")
            #     plot = np.round(self._validation_losses.copy(), decimals=5)
            #     plt.plot(plot[1:], color="red")
            #     plot = np.round(self._validation_f1.copy(), decimals=5)
            #     plt.plot(plot[1:], color="green")
            #     plt.legend(['training', 'validation','validation_f1'], loc='upper left')
            #     plt.show()

            if self._should_stop():
                break

        print("end of run")
        print("start to plot")
        # plot = np.round(self._train_loss.copy(), decimals=5)
        # plt.plot(plot[1:])
        # plot = np.round(self._validation_losses.copy(), decimals=5)
        # plt.plot(plot[1:])
        # plot = np.round(self._validation_f1.copy(), decimals=5)
        # plt.plot(plot[1:], color="green")
        # plt.legend(['training', 'validation','validation_f1'], loc='upper left')
        # plt.show()


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

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('distance.pdf')
print("Setup Complete")

mileposts = np.array([0, 198, 303, 736, 871, 1175, 1475, 1544,
                      1913, 2448])
distance_array = np.abs(mileposts - mileposts[:, np.newaxis])
print(distance_array)

x, y = np.arange(5), np.arange(5)[:, np.newaxis]
distance = np.sqrt(x ** 2 + y ** 2)
print(distance)
plt.pcolor(distance)
plt.colorbar()

pp.savefig()
plt.clf()

pp.close()

x, y = np.ogrid[0:5, 0:5]
print(x, y)
print(x.shape, y.shape)
distance = np.sqrt(x ** 2 + y ** 2)
x, y = np.mgrid[0:4, 0:4]
print(x)
print(y)


text:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

#import shapes.conic_sections as conic
import plotshapes as sh

mpl.rc('axes',labelsize=24)
mpl.rc('font',size=24)

print('This is the test directory')

#C1 = sh.conic_sections.circle(3,(5,-2.5))
C1 = sh.conic_sections.circle(1,(-2,3))

E1 = sh.conic_sections.ellipse((0.25,1),(5,0))
#E1.rotate(-70)
E2 = sh.conic_sections.ellipse((3,1),(-5,-5))

R1 = sh.quadrilateral.rectangle((4,0.5),(0,3))

#R1.rotate_about_center(135)

print('Circumference = %1.2f ; Area = %1.2f' %(C1.perimeter, C1.area))

fig1 = plt.figure('Test-shapes-pkg')
ax1_f1 = fig1.add_subplot(111)
#ax1_f1.plot(C1.x,C1.y,linewidth=2)
#ax1_f1.plot(E1.x,E1.y,linewidth=2)
#ax1_f1.plot(E2.x,E2.y,linewidth=2)
#ax1_f1.plot(R1.x,R1.y,linewidth=2)
#ax1_f1.plot(R2.x,R2.y,linewidth=2)
N = 8
for count in range(N):
	R1.plot(ax1_f1,text_tag = str(count*360/N))
	R1.rotate_about_center(360/N)
	E1.plot(ax1_f1,text_tag = str(count*360/N))
	E1.rotate_about_center(360/N)

C2 = sh.conic_sections.circle()
C2.plot(ax1_f1)

ax1_f1.axis('equal')
#ax1_f1.legend()

ax1_f1.grid(1)
ax1_f1.set_xlim([-10,10])
ax1_f1.set_ylim([-10,10])

plt.show()

max_stars_repo_name: sarang-IITKgp/plot-shapes

max_issues_repo_name: sarang-IITKgp/plot-shapes

max_forks_repo_name: sarang-IITKgp/plot-shapes