text:
import numpy as np
import tensorflow as tf
import unittest

from abi.misc.dataset import Dataset
from abi.models.rnnvae import RNNVAE

class TestRNNVAE(unittest.TestCase):

    def setUp(self):
        # reset graph before each test case
        tf.reset_default_graph()

    def test_rnn_vae_simple(self):
        # build model
        max_len = 5
        obs_dim = 1
        act_dim = 2
        z_dim = 2
        batch_size = 2
        kl_final = 0.0
        kl_initial = 0.0
        model = RNNVAE(
            max_len,
            obs_dim,
            act_dim,
            batch_size,
            z_dim=z_dim,
            kl_steps=100,
            kl_final=kl_final,
            kl_initial=kl_initial
        )

        # generate some data
        # z is the latent variable
        # act is a deterministic function of z
        # obs is zeros
        # thus latent space has to be used
        n_samples = batch_size
        z = np.random.randn(n_samples, act_dim)
        obs = np.zeros((n_samples, max_len, obs_dim))
        act = np.ones((n_samples, max_len, act_dim)) * z.reshape(n_samples, 1, act_dim)
        lengths = np.array([max_len, 3])
        dataset = Dataset(obs, act, lengths, batch_size, shuffle=False)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            model.train(dataset, n_epochs=500, verbose=True)
            info = model.reconstruct(obs, act, lengths)

        self.assertTrue(info['data_loss'] < -2)

    def test_get_set_param_values(self):
        with tf.Session() as sess:
            model = RNNVAE(5, 2, 2, 10)
            sess.run(tf.global_variables_initializer())
            params = model.get_param_values()
            init_sum = np.sum([np.sum(p) for p in params])
            params = [p * 0 for p in params]
            model.set_param_values(params)
            new_params = model.get_param_values()
            new_sum = np.sum([np.sum(p) for p in new_params])
            orig_shapes = [np.shape(p) for p in params]
            new_shapes = [np.shape(p) for p in new_params]
            np.testing.assert_array_equal(orig_shapes, new_shapes)
            self.assertNotEqual(init_sum, new_sum)
            self.assertEqual(new_sum, 0.)

    @unittest.skipIf(__name__ != '__main__', 'run this test directly')
    def test_rnn_vae_plot(self):
        # build model
        max_len = 5
        obs_dim = 1
        act_dim = 2
        z_dim = 2
        batch_size = 100
        model = RNNVAE(
            max_len,
            obs_dim,
            act_dim,
            batch_size,
            z_dim=z_dim,
            kl_steps=100
        )

        # generate some data
        # z is the latent variable
        # act is a deterministic function of z
        # obs is zeros
        # thus latent space has to be used
        n_samples = 1000
        z = np.random.randn(n_samples, act_dim)
        obs = np.zeros((n_samples, max_len, obs_dim))
        act = np.ones((n_samples, max_len, act_dim)) * z.reshape(n_samples, 1, act_dim)
        lengths = np.random.randint(2, max_len, n_samples)
        dataset = Dataset(obs, act, lengths, batch_size, shuffle=False)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            model.train(dataset, n_epochs=20, verbose=True)
            info = model.reconstruct(obs, act, lengths)

        plot = True
        if plot:
            import matplotlib
            matplotlib.use('TkAgg')
            import matplotlib.pyplot as plt
            plt.subplot(1,2,1)
            plt.scatter(act[:,-1,0], act[:,-1,1], c=z.sum(1) / act_dim)
            plt.subplot(1,2,2)
            plt.scatter(act[:,-1,0], act[:,-1,1], c=info['mean'].sum(1) / act_dim)
            plt.show()

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


text:
#!/usr/bin/python

# encoding: utf-8

"""
@desc: 模型推理部分，本地载入模型推理
"""
import os
import grpc
import codecs
import pickle
import warnings
import numpy as np

import tensorflow as tf
from bert import tokenization
from public_tools.ner_utils import get_entity, get_result

class InputFeatures(object):
    """A single set of features of msra_data."""
    def __init__(self, input_ids, input_mask, segment_ids, label_ids):
        self.input_ids = input_ids
        self.input_mask = input_mask
        self.segment_ids = segment_ids
        self.label_ids = label_ids

class Args():
    def __init__(self):
        self.kflod = 2
        self.max_seq_length = 128
        self.is_training =False
        self.use_one_hot_embeddings = False
        self.batch_size = 1
        self.dev_file = './data/clue_ner/dev.txt'
        self.dev_file_json = './data/clue_ner/dev.json'
        self.dev_result_file = './data/clue_ner/submit/cluener_predict_dev.json'
        self.test_file = './data/clue_ner/test.json'
        self.test_result_file = './data/clue_ner/submit/cluener_predict.json'

        self.bert_config_file = 'D:/Expriment/pretrain_model_tf/bert/bert_config.json'
        self.output_dir = 'D:/Expriment/model_output/ner_tool/bert_wol/single_task/clue_ner/runs/checkpoints'
        self.vocab_file = 'D:/Expriment/pretrain_model_tf/bert/vocab.txt'
        self.label2id_dic_dir = 'D:/Expriment/model_output/ner_tool/bert_wol/single_task/clue_ner/runs/checkpoints'
        self.export_dir = "D:/Expriment/model_output/ner_tool/bert_wol/single_task/clue_ner/exported_model/1/1622539079"

class InferenceBase(object):
    def __init__(self, vocab_file, label2id_dic_dir, url=None, model_name=None,
                 signature_name=None, export_dir=None, do_lower_case=True):
        """
        预测的基类，分为两种方式预测
            a. grpc 请求方式
            b. 本地导入模型方式

        :arg
        vocab_file: bert 预训练词典的地址，这里在 'chinese_L-12_H-768_A-12/vocab.txt '中
        labels: str 或 list 类型，需要被转化为id的label，当为str类型的时候，即为标签-id的pkl文件名称；
                当为list时候，即为标签列表。
        url: string类型，用于调用模型测试接口，host:port，例如'10.0.10.69:8500'
        export_dir: string类型，模型本地文件夹目录，r'model\1554373222'
        model_name: string类型，tensorflow serving 启动的时候赋予模型的名称，当
                    url被设置的时候一定要设置。
        signature_name: string类型，tensorflow serving 的签名名称，当
                    url被设置的时候一定要设置。
        do_lower_case: 是否进行小写处理

        :raise
        url和export_dir至少选择一个，当选择url的时候，model_name和signature_name不能为
        None。
        """
        self.url = url
        self.export_dir = export_dir

        if export_dir:
            self.predict_fn = tf.contrib.predictor.from_saved_model(self.export_dir)
        print('模型加载完毕！')
        self.tokenizer = tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case)
        print('Tokenizer加载完毕！')
        # 获取id2char字典
        self.id2char = self.tokenizer.inv_vocab
        self.id2label = self._load_id_map_label(label2id_dic_dir)
        print('标签信息加载完毕！')

    def local_infer(self, examples):
        """
        导入本地的PB文件进行预测
        """
        pass

    def tf_serving_infer(self, examples):
        """
        使用tensorflow serving进行grpc请求预测
        """
        pass

    def preprocess(self, sentences, max_seq_length):
        pass

    def create_example(self):
        pass

    @staticmethod
    def _load_id_map_label(label2id_dic_dir):
        # 加载label->id的词典
        with codecs.open(os.path.join(label2id_dic_dir, 'label2id.pkl'), 'rb') as rf:
            label2id = pickle.load(rf)
            id2label = {value: key for key, value in label2id.items()}

        return id2label

class NerInfer(InferenceBase):
    def __init__(self, vocab_file, label2id_dic_dir, url=None, model_name=None,
                 signature_name=None, export_dir=None, do_lower_case=True):
        """
        bert ner, 参数解释查看 `InferenceBase`
        """
        super(NerInfer, self).__init__(vocab_file, label2id_dic_dir, url, model_name, signature_name, export_dir, do_lower_case)

    def preprocess(self, sentences, max_seq_length):
        """
        对sentences进行预处理，并生成examples

        :arg
        sentences: ['北京是中国的首都']
        max_seq_length: 输入的每一个句子的最大长度

        :return
        examples: tf.train.Example对象
        feature: 标准化数据后的InputFeatures类
        """
        if not sentences or not isinstance(sentences, list):
            raise ValueError('`sentences` must be list object and not a empty list !')

        examples = []
        sentence = sentences[0]
        feature = self.convert_single_example(sentence, max_seq_length)
        features = dict()
        features['input_ids'] = tf.train.Feature(int64_list=tf.train.Int64List(value=feature.input_ids))
        features['input_mask'] = tf.train.Feature(int64_list=tf.train.Int64List(value=feature.input_mask))
        features['segment_ids'] = tf.train.Feature(int64_list=tf.train.Int64List(value=feature.segment_ids))
        features['label_ids'] = tf.train.Feature(int64_list=tf.train.Int64List(value=feature.label_ids))
        example = tf.train.Example(features=tf.train.Features(feature=features))
        examples.append(example.SerializeToString())
        return examples, feature

    def convert_single_example(self, example, max_seq_length):
        """
        将一个样本进行分析，然后将字转化为id, 标签转化为id,然后结构化到InputFeatures对象中
        :param example: 一个样本
        :param max_seq_length:
        :return: InputFeatures对象
        """
        tokens = self.tokenizer.tokenize(example)
        # tokens = tokenizer.tokenize(example.text)
        # 序列截断
        if len(tokens) >= max_seq_length - 1:
            tokens = tokens[0:(max_seq_length - 2)]  # -2 的原因是因为序列需要加一个句首和句尾标志
        ntokens = []
        segment_ids = []
        ntokens.append("[CLS]")  # 句子开始设置CLS 标志
        segment_ids.append(0)
        # append("O") or append("[CLS]") not sure!
        for i, token in enumerate(tokens):
            ntokens.append(token)
            segment_ids.append(0)
        ntokens.append("[SEP]")  # 句尾添加[SEP] 标志
        segment_ids.append(0)
        # append("O") or append("[SEP]") not sure!
        input_ids = self.tokenizer.convert_tokens_to_ids(ntokens)  # 将序列中的字(ntokens)转化为ID形式
        input_mask = [1] * len(input_ids)

        # padding, 使用
        while len(input_ids) < max_seq_length:
            input_ids.append(0)
            input_mask.append(0)
            segment_ids.append(0)
            ntokens.append("**NULL**")
            # label_mask.append(0)
        # print(len(input_ids))
        assert len(input_ids) == max_seq_length
        assert len(input_mask) == max_seq_length
        assert len(segment_ids) == max_seq_length
        # assert len(label_mask) == max_seq_length

        # 结构化为一个类
        feature = InputFeatures(
            input_ids=input_ids,
            input_mask=input_mask,
            segment_ids=segment_ids,
            label_ids=[8],
            # label_mask = label_mask
        )
        return feature

    def infer(self, sentences, max_seq_length):
        """
        对外的测试接口

        :arg
        sentences:  二维列表，即输入的句子，输入有一下要求：
                （1）可以是一段话，但是每个句子最好小于64个字符串长度
                （2）长度不可以小于2
        max_seq_length: 输入的每一个句子的最大长度

        sentences_entities: 返回每一个句子的实体
        """
        examples, feature = self.preprocess(sentences, max_seq_length)
        predictions = self.local_infer(examples)
        print(predictions)

        # y_pred = predictions['predictions']
        y_pred = predictions['output']
        print(y_pred)

        sentences_entities = self.get_entity_result(feature, self.id2char, self.id2label, y_pred)

        return sentences_entities

    def local_infer(self, examples):
        """
        本地进行预测，参数解释同上
        """
        predictions = self.predict_fn({'examples': examples})

        return predictions

    @staticmethod
    def get_entity_result(feature, id2char, id2label, y_pred):
        """
        提取实体

        :arg
        tokens: 二维列表，句子处理后得到的token
        tags: 二维列表，预测的结果
        sentences_index: 二维列表，句子拆分后，对应到原句的index

        :return
        sentences_entities: 二维列表，返回实体结果，例如[('昆凌', 'PER')...]
        """
        sent_tag = []
        y_pred_clean = []
        input_ids_clean = []
        # 去除 [CLS] 和 [SEP]获取正确的tag范围
        print([id2char[i] for i in feature.input_ids])
        print(len(feature.input_ids))
        print(y_pred[0][0])
        print([id2label[i] for i in list(y_pred[0])])
        print(len(list(y_pred[0])))
        for index, id in enumerate(feature.input_ids):
            char = id2char[id]
            tag = id2label[list(y_pred[0])[index]]
            if char == "[CLS]":
                continue
            if char == "[SEP]":
                break
            input_ids_clean.append(id)
            sent_tag.append(tag)
            y_pred_clean.append(list(y_pred[0])[index])

        sent_tag = ' '.join(sent_tag)
        print(sentence + '\n' + sent_tag)
        entity = get_entity([sentence], [y_pred_clean], id2label)
        print('predict_result:')
        print(entity)
        return entity

if __name__ == '__main__':
    args = Args()
    nerinfer = NerInfer(vocab_file=args.vocab_file, label2id_dic_dir=args.label2id_dic_dir, export_dir=args.export_dir, do_lower_case=True)

    # while True:
    #     # 或者可以直接登陆美国使馆的网站来查询
    #     # sentence = input('请输入句子：')
    #     sentence = '或者可以直接登陆美国使馆的网站来查询'
    #     print(nerinfer.infer([sentence], args.max_seq_length))
    sentence = '或者可以直接登陆美国使馆的网站来查询'
    print(nerinfer.infer([sentence], args.max_seq_length))

ext: py

max_stars_repo_name: WenRichard/NER-tf

max_issues_repo_name: WenRichard/NER-tf

max_forks_repo_name: WenRichard/NER-tf

reason: import numpy


text:
import numpy as np
import torch.nn.functional

from .symext import *
from .upsample2df import *
from scipy import signal
from .atrous import *

def nsfbdec(x, h0, h1, lev, gpu_mode=False):
    if lev != 0:
        l2 = np.eye(2)
        shift = (-2**(lev-1)) * np.array([1, 1]) + 2
        L = 2**lev

        y0 = upsample2df(h0, lev, gpu_mode=gpu_mode)
        y0 = symext(x, y0, shift, gpu_mode=gpu_mode)
        y0 = atrous(y0, h0, l2 * L, gpu_mode=gpu_mode)

        y1 = upsample2df(h1, lev, gpu_mode=gpu_mode)
        y1 = symext(x, y1, shift, gpu_mode=gpu_mode)
        y1 = atrous(y1, h1, l2 * L, gpu_mode=gpu_mode)
    else:
        shift = [1, 1]
        y0 = symext(x, h0, shift, gpu_mode=gpu_mode)
        if gpu_mode:
            y0 = torch.nn.functional.conv2d(y0, h0)
            y0 = y0
        else:
            y0 = signal.convolve2d(y0, h0, 'valid')

        y1 = symext(x, h1, shift, gpu_mode=gpu_mode)
        if gpu_mode:
            y1 = torch.nn.functional.conv2d(y1, h1)
            y1 = y1
        else:
            y1 = signal.convolve2d(y1, h1, 'valid')
    return y0, y1

max_stars_repo_name: yilinshao/CoT-Contourlet-Transformer

max_issues_repo_name: yilinshao/CoT-Contourlet-Transformer

max_forks_repo_name: yilinshao/CoT-Contourlet-Transformer

reason: import numpy,from scipy


text:
# Satellite equations of motion

# -----------------------------------------------------------------------------
# Global Variables
# -----------------------------------------------------------------------------
# Palettes for categorical data visualisation
global paletteAccent6, paletteDark6
paletteAccent6 = ["#7fc97f", "#beaed4", "#fdc086", "#ffff99", "#386cb0", "#f0027f"]
paletteDark6 = ["#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02"]
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import math as mt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as colors
# plt.rc('text', usetex=True)
# plt.rc('font', family='serif')
import pulp
import importlib

# -----------------------------------------------------------------------------
# Functions
# -----------------------------------------------------------------------------
# TO DO
# -----
# Determine satellite state (impinged, free, restricted)
# Implement tex in plots
# Automatically set legend positions
# -----------------------------------------------------------------------------

# --------
# Plotting
# --------

# Plot rectangle in figure. Allow choice of line style
def plotRectangle(axes, left, bottom, width, height, linestyle):
  linestyles = {
    "solid": '-',
    "dashed": '--',
    "dotted": ':',
  }
  linestyle = linestyles[linestyle]

  rect = patches.Rectangle((left, bottom), width, height, fill=None, alpha=1, linestyle=linestyle)
  axes.add_patch(rect)
  return

def plotDot(x, y, style):
  plt.plot(x, y, style)
  return

def plotPath(title, testCase, save, x, objects, T, V, N, colourMap):

  if N == 1: # Need to create function!
    fig = plot1DPath(title, testCase, save, x, objects, T, V, N, colourMap)
  elif N == 2:
    fig = plot2DPath(title, testCase, save, x, objects, T, V, N, colourMap)
  # elif N == 3:
  # else:
  return fig

# Plot Path of satellites in 2D space
def plot1DPath(title, testCase, save, x, objects, T, V, N, colourMap):

  # Variables
  title = append([title, " 1D Path"])
  fig = plt.figure(title)
  axes = plt.gca()
  d = np.empty([T, V, N])
  colourMap = globals()[colourMap]

  for i in range(T):
    for p in range(V):
      for n in range(N):
        d[i, p, n] = x[i, p, n].varValue
  for p in range(V):
    colour = colourMap[p]
    # Start and end positions
    plt.plot(d[0, p, 0], 0,
      marker='+',
      color=colour,
      linestyle='None')
    plt.plot(d[T-1, p, 0], 0,
      marker='x',
      color=colour,
      linestyle='None')
    # Satellite Path
    for i in range(1, T-1):
      plt.plot(d[i, p, 0], 0,
        marker='o',
        color=colour,
        linestyle='None')

    # Plot layout
  axisRange = setAxis(N, d)
  #axes.set_xlim(axisRange[0,:])
  plt.legend()
  fig.suptitle(title, fontsize=12)

  # Save
  if(save):
    figName = append(["figures/", testCase, "_1DPath.png"])
    plt.savefig(figName, bbox_inches='tight')
  return fig

# Plot Path of satellites in 2D space
def plot2DPath(title, testCase, save, x, objects, T, V, N, colourMap):

  # Variables
  title = append([title, " 2D Path"])
  fig = plt.figure(title)
  axes = plt.gca()
  d = np.empty([T, V, N])
  colourMap = globals()[colourMap]

  for i in range(T):
    for p in range(V):
      for n in range(N):
        d[i, p, n] = x[i, p, n].varValue
  for p in range(V):
    colour = colourMap[p]
    # Start and end positions
    #label = "$x_{{(0, {}, :)}}$".format(p)
    plt.plot(d[0, p, 0], d[0, p, 1],
      marker='+',
      color=colour,
      linestyle='None')
    #label = "$x_{{(T-1, {}, :)}}$".format(p)
    plt.plot(d[T-1, p, 0], d[T-1, p, 1],
      marker='x',
      color=colour,
      linestyle='None')
    # Satellite Path
    label = "$x_{{(:, {}, :)}}$".format(p)
    cm = plt.cm.get_cmap('Reds')
    t = np.arange(T)
    plt.scatter(d[0:T, p, 0], d[0:T, p, 1] ,
      linewidth=2,
      marker = 'o',
      c=t,
      cmap=cm,
      label=label)
  if objects.shape[1] > 0:
    for i in range(objects.shape[0]):
      left   = objects[i, 0]
      bottom = objects[i, 1]
      width  = objects[i, 2] - objects[i, 0]
      height = objects[i, 3] - objects[i, 1]
      plotRectangle(axes, left, bottom, width, height, "solid") # bottom, left,

  # Plot layout
  plt.grid(color='k', linestyle=':', alpha=0.5)
  axisRange = setAxis(N, d)
  axes.set_xlim(axisRange[0,:])
  axes.set_ylim(axisRange[1,:])
  plt.legend()
  fig.suptitle(title, fontsize=12)

  # Save
  if(save):
    figName = append(["figures/", testCase, "_2DPath.png"])
    plt.savefig(figName, bbox_inches='tight')
  return fig

# Plot thrusts of satellites
def plotThrust(title, testCase, save, u, T, V, N, colourMap):
  # TO DO:
  # Bar charts for each dimension, or sum fuel consumption?

  # Variables
  title = append([title, " thrust profile"])
  fig = plt.figure(title)
  thrust = np.empty([T, V, N])
  colourMap = globals()[colourMap]

  # Get thrust values
  for i in range(T):
    for p in range(V):
      for n in range(N):
        thrust[i, p, n] = u[i, p, n].varValue

  # Fuel consumption plots
  for n in range(N):
    for p in range(V):
      colour = colourMap[p]
      timeAx = np.linspace(0, T, num=T)
      axes = plt.subplot(N,1,n+1)
      axes.bar(timeAx, thrust[:, p, n], align='center', color=colour)
      plt.grid(color='k', linestyle=':', alpha=0.5)

  fig.suptitle(title, fontsize=12)

  # Create separate function for this
  if(save):
    figName = append(["figures/", testCase, "_plotThrust.png"])
    plt.savefig(figName, bbox_inches='tight')
  return fig

# Plot thrusts of satellites
def plotState(title, testCase, save, x, T, V, N, colourMap):

  # Variables
  title = append([title, " state profile"])
  fig = plt.figure(title)
  dis = np.empty([T, V, N])
  vel = np.empty([T, V, N])
  colourMap = globals()[colourMap]

  # Get thrust values
  for i in range(T):
    for p in range(V):
      for n in range(N):
        dis[i, p, n] = x[i, p, n].varValue
        vel[i, p, n] = x[i, p, n+N].varValue

  # Fuel consumption plots
  for n in range(N):
    for p in range(V):
      colour = colourMap[p]
      timeAx = np.linspace(0, T, num=T)
      axes = plt.subplot(N,1,n+1)
      axes.bar(timeAx, thrust[:, p, n], align='center', color=colour)
      plt.grid(color='k', linestyle=':', alpha=0.5)

  fig.suptitle(title, fontsize=12)

  # Create separate function for this
  if(save):
    figName = append(["figures/", testCase, "_plotThrust.png"])
    plt.savefig(figName, bbox_inches='tight')
  return fig

# -------
# Outputs
# -------
# Print report of decision variables for optimized solution
def printOptimizationResults(title, model, x, u, T, V, N, pad, dp):
  if pad <= 5:
    print("ERROR: pad must be > 5")
    return
  if dp >= pad:
    print("ERROR: dp must be < pad")
    return

  padSpace = pad - 5

  print("-"*79)
  print(title)
  print("Status: {}".format(pulp.LpStatus[model.status]))
  print("Objective: {}".format(pulp.value(model.objective)))
  print("-"*79)

  if pulp.LpStatus[model.status] != "Infeasible":
    print("States")
    print("-"*79)
    print("Timestep {:{pad}}| Vehicle {:{pad}}| Dimension {:{pad}}| Displacement{:{pad}}| Velocity{:{pad}}|".format("", "", "", "", "", pad=padSpace))
    for i in range(T):
      for p in range(V):
        for n in range(N):
          dis = x[i, p, n]
          dis_val = dis.varValue
          vel = x[i, p, N+n]
          vel_val = vel.varValue
          print("  {:{pad}}  |  {:{pad}}  |   {:{pad}}   |    {:{pad}.{dp}f}    |  {:{pad}.{dp}f}  |".format(i, p, n, dis_val, vel_val, pad=pad, dp=dp))
    print("-"*79)
    print("Inputs")
    print("-"*79)
    print("Timestep {:{pad}}| Vehicle {:{pad}}| Dimension {:{pad}}| Fuel        {:{pad}}|".format("", "", "", "", pad=padSpace))
    for i in range(T):
      for p in range(V):
        for n in range(N):
          fuel = u[i, p, n]
          fuel_val = fuel.varValue
          print("  {:{pad}}  |  {:{pad}}  |   {:{pad}}   |    {:{pad}.{dp}f}    |".format(i, p, n, fuel_val, pad=pad, dp=dp))
    print("-"*79)
  return

# Store results in Pandas dataframe and export as csv
def exportCSV():
  return

# ------------------
# Optimization Model
# ------------------
# Return a list of constraints for model using free space dynamics
def freeSpaceDynamics(x, u, T, V, N, m, del_t):
  retVal = []
  for i in range(1, T): # First time step, no dynamics
    for p in range(V):
      for n in range(N):
        retVal.append(
          x[i, p, N+n] == x[i-1, p, N+n] + u[i-1, p, n]*1/m[p])
        retVal.append(
          x[i, p, n] == x[i-1, p, n]
                      + x[i-1, p, N+n]*del_t
                      + 0.5*u[i-1, p, n]*1/m[p]*del_t*del_t)
  return retVal

def linearHillsDynamics(x, u, T, V, N, m, del_t):
  retVal = []
  return retVal

# -------------
# Miscellaneous
# -------------
# Appends str2 to str1
def append(strArr):
  mainStr = strArr[0]
  if(len(strArr)) > 0:
    for i in range(1, len(strArr)):
      appendStr = str(strArr[i])
      for j in range(len(appendStr)):
        mainStr += appendStr[j]
  return mainStr

# Return axis limits for a dataset
# TO DO:
# - Include scale factor?
def setAxis(N, data):
  axisRange = np.empty([N, 2])
  bufferSpace = 0.5
  scaleFactor = 1.2
  for n in range(N):
    axisRange[n, 0] = data[:, :, n].min() - bufferSpace
    axisRange[n, 1] = data[:, :, n].max() + bufferSpace
  return axisRange

def totalConstraints(T, V, N, numObjects):
  # Constraints up to obstacle avoidance (check!)
  numConstraints = 4*T*V*N + 2*(T-1)*V*N + 2*V*N + T*V*numObjects + T*V*N*numObjects
  return numConstraints

# -----------------------------------------------------------------------------
# Optimization Problem
# -----------------------------------------------------------------------------
# TO DO:
# ------
# - Hill's equations
# - Implement defined software architecture
# - Implement fuel use - currently optimizing for minimum total thrust
# -----------------------------------------------------------------------------

def optimizeTrajectory(N, T, T_end, V, P, W, M, m, TSFC, minApproachDist, x_ini, x_fin, x_lim, u_lim, objects, r):

  # --------------------
  # Calculated Variables
  # --------------------
  del_t = T_end/T
  numObjects = objects.shape[0]

  # ------------------
  # Decision variables
  # ------------------
  # Thrust
  u = pulp.LpVariable.dicts(
    "input", ((i, p, n) for i in range(T) for p in range(V) for n in range(N)),
    cat='Continuous')
  # Thrust Magnitude
  v = pulp.LpVariable.dicts(
    "inputMag", ((i, p, n) for i in range(T) for p in range(V) for n in range(N)),
    lowBound=0,
    cat='Continuous')
  # State
  x = pulp.LpVariable.dicts(
    "state", ((i, p, k) for i in range(T) for p in range(V) for k in range(2*N)),
    cat='Continuous')
  # Object collision
  a = pulp.LpVariable.dicts(
    "objCol", ((i, p, l, k) for i in range(T) for p in range(V) for l in range(numObjects) for k in range(2*N)),
    cat='Binary')
  # Satellite Collision Avoidance
  b = pulp.LpVariable.dicts(
    "satelliteCollisionAvoidance", ((i, p, q, k) for i in range(T) for p in range(V) for q in range(V) for k in range(2*N)),
    cat='Binary')
  # Plume Impingement
  c_plus = pulp.LpVariable.dicts(
    "plumeImpingementPositive", ((i, p, q, n, k) for i in range(T) for p in range(V) for q in range(V) for n in range(N) for k in range(2*N)),
    cat='Binary')
  c_minus = pulp.LpVariable.dicts(
    "plumeImpingementNegative", ((i, p, q, n, k) for i in range(T) for p in range(V) for q in range(V) for n in range(N) for k in range(2*N)),
    cat='Binary')

  # ------------------
  # Optimization Model
  # ------------------

  # Instantiate Model
  model = pulp.LpProblem("Satellite Fuel Minimization Problem", pulp.LpMinimize)
  # Objective Function
  model += pulp.lpSum(v[i, p, n] for i in range(T) for p in range(V) for n in range(N)), "Fuel Minimization"

  # -----------
  # Constraints
  # -----------

  # Basic Constraints
  # -----------------
  # Constrain thrust magnitude to abs(u[i, p, n])
  for i in range(T):
    for p in range(V):
      for n in range(N):
        model +=  u[i, p, n] <= v[i, p, n]
        model += -u[i, p, n] <= v[i, p, n]
  # State and Input vector start and end values
  for p in range(V):
    for k in range(2*N):
      model += x[0, p, k]   == x_ini[p, k]
      model += x[T-1, p, k] == x_fin[p, k]
  # Model Dynamics
  for constraint in freeSpaceDynamics(x, u, T, V, N, m, del_t):
    model += constraint
  # State and Input vector limits
  for i in range(T):
    for p in range(V):
      for n in range(N):
        model += u[i, p, n] <=  u_lim[p, n]
        model += u[i, p, n] >= -u_lim[p, n]
      for k in range(2*N): # Necessary?
        model += x[i, p, k] <=  x_lim[p, k]
        model += x[i, p, k] >= -x_lim[p, k]

  # Obstacle Avoidance
  # ------------------
  if objects.shape[1] > 0:
    for i in range(T):
      for p in range(V):
        for l in range(numObjects):
          model += pulp.lpSum(a[i, p, l, n] for n in range(2*N)) <= 2*N-1
          for n in range(N):
            model += x[i, p, n] >= objects[l, N+n] + minApproachDist - M*a[i, p, l, N+n]
            model += x[i, p, n] <= objects[l, n] - minApproachDist + M*a[i, p, l, n]

  # Collision Avoidance
  # -------------------
  if V > 1: # If more than one vehicle
      for i in range(T):
        for p in range(V):
          for q in range(V):
            if q > p:
              model += pulp.lpSum(b[i, p, q, k] for k in range(2*N)) <= 2*N-1
              for n in range(N):
                model += x[i, p, n] - x[i, q, n] >= r[n] - M*b[i, p, q, n]
                model += x[i, q, n] - x[i, p, n] >= r[n] - M*b[i, p, q, n+N]

  # Plume Impingement
  # -----------------
  # Positive thrust
  if V > 1: # If more than one vehicle
      for i in range(T):
        for p in range(V):
          for q in range(V):
            if q != p:
              for n in range(N):
                model += pulp.lpSum(c_plus[i, p, q, n, k] for k in range(2*N)) <= 2*N
                model += -u[i, p, n] >= - M*c_plus[i, p, q, n, 0]
                model += x[i, p, n] - x[i, q, n] >= P - M*c_plus[i, p, q, n, n]
                model += x[i, q, n] - x[i, p, n] >= - M*c_plus[i, p, q, n, n+N]
                for m in range(N):
                  if m != n:
                    x[i, p, m] - x[i, q, m] >= W - M*c_plus[i, p, q, n, m]
                    x[i, q, m] - x[i, p, m] >= W - M*c_plus[i, p, q, n, m+N]
  # Negative thrust
      for i in range(T):
        for p in range(V):
          for q in range(V):
            if q != p:
              for n in range(N):
                model += pulp.lpSum(c_minus[i, p, q, n, k] for k in range(2*N)) <= 2*N
                model += u[i, p, n] >= - M*c_minus[i, p, q, n, 0]
                model += x[i, p, n] - x[i, q, n] >= - M*c_minus[i, p, q, n, n]
                model += x[i, q, n] - x[i, p, n] >= P - M*c_minus[i, p, q, n, n+N]
                for m in range(N):
                  if m != n:
                    x[i, p, m] - x[i, q, n] >= W - M*c_minus[i, p, q, n, m]
                    x[i, q, m] - x[i, p, m] >= W - M*c_minus[i, p, q, n, m+N]

  # Plume Avoidance for Vehicles
  # ----------------------------

  # Plume Avoidance for Obstacles
  # -----------------------------

  # Final Configuration Selection
  # -----------------------------

  # Solve model and return results in dictionary
  model.solve(pulp.CPLEX())

  # Create Pandas dataframe for results and return

  return {'model':model, 'x':x, 'u':u}

# --------------
# Run test cases
# --------------
testCases = ["BUGFIX03"]

for test in testCases:
  file = importlib.import_module(test)
  solution = optimizeTrajectory(file.N, file.T, file.T_end, file.V, file.P, file.W, file.M, file.m, file.TSFC, file.minApproachDist, file.x_ini, file.x_fin, file.x_lim, file.u_lim, file.objects, file.r)

  printOptimizationResults(file.title, solution['model'], solution['x'], solution['u'], file.T, file.V, file.N, 8, 3)
  fig = plotPath(file.title, test, False, solution['x'], file.objects, file.T, file.V, file.N, "paletteDark6")
  fig = plotThrust(file.title, test, False, solution['u'], file.T, file.V, file.N, "paletteDark6")

plt.show()


text:
from typing import List, Union

import numpy
import torch
from scipy.interpolate import interp1d
from scipy.optimize import brentq
from sklearn.metrics import roc_curve

def get_all_EERs(
    preds: Union[torch.Tensor, List, numpy.ndarray], keys: List
) -> List[float]:
    """
    Calculate all three EERs used in the SASV Challenge 2022.
    preds and keys should be pre-calculated using dev or eval protocol in
    either 'protocols/ASVspoof2019.LA.asv.dev.gi.trl.txt' or
    'protocols/ASVspoof2019.LA.asv.eval.gi.trl.txt'

    :param preds: list of scores in tensor
    :param keys: list of keys where each element should be one of
    ['target', 'nontarget', 'spoof']
    """
    sasv_labels, sv_labels, spf_labels = [], [], []
    sv_preds, spf_preds = [], []

    for pred, key in zip(preds, keys):
        if key == "target":
            sasv_labels.append(1)
            sv_labels.append(1)
            spf_labels.append(1)
            sv_preds.append(pred)
            spf_preds.append(pred)

        elif key == "nontarget":
            sasv_labels.append(0)
            sv_labels.append(0)
            sv_preds.append(pred)

        elif key == "spoof":
            sasv_labels.append(0)
            spf_labels.append(0)
            spf_preds.append(pred)
        else:
            raise ValueError(
                f"should be one of 'target', 'nontarget', 'spoof', got:{key}"
            )

    fpr, tpr, _ = roc_curve(sasv_labels, preds, pos_label=1)
    sasv_eer = brentq(lambda x: 1.0 - x - interp1d(fpr, tpr)(x), 0.0, 1.0)

    fpr, tpr, _ = roc_curve(sv_labels, sv_preds, pos_label=1)
    sv_eer = brentq(lambda x: 1.0 - x - interp1d(fpr, tpr)(x), 0.0, 1.0)

    fpr, tpr, _ = roc_curve(spf_labels, spf_preds, pos_label=1)
    spf_eer = brentq(lambda x: 1.0 - x - interp1d(fpr, tpr)(x), 0.0, 1.0)

    return sasv_eer, sv_eer, spf_eer

def get_all_EERs_my(
    preds: Union[torch.Tensor, List, numpy.ndarray], keys: List
) -> List[float]:
    """
    Calculate all three EERs used in the SASV Challenge 2022.
    preds and keys should be pre-calculated using dev or eval protocol in
    either 'protocols/ASVspoof2019.LA.asv.dev.gi.trl.txt' or
    'protocols/ASVspoof2019.LA.asv.eval.gi.trl.txt'

    :param preds: list of scores in tensor
    :param keys: list of keys where each element should be one of
    ['target', 'nontarget', 'spoof']
    """
    sasv_labels, sv_labels, spf_labels = [], [], []
    sv_preds, spf_preds = [], []

    for pred, key in zip(preds, keys):
        if key == 0:
            sasv_labels.append(1)
            sv_labels.append(1)
            spf_labels.append(1)
            sv_preds.append(pred)
            spf_preds.append(pred)

        elif key == 1:
            sasv_labels.append(0)
            sv_labels.append(0)
            sv_preds.append(pred)

        elif key == 2:
            sasv_labels.append(0)
            spf_labels.append(0)
            spf_preds.append(pred)
        else:
            raise ValueError

    fpr, tpr, _ = roc_curve(sasv_labels, preds, pos_label=1)
    sasv_eer = brentq(lambda x: 1.0 - x - interp1d(fpr, tpr)(x), 0.0, 1.0)

    fpr, tpr, _ = roc_curve(sv_labels, sv_preds, pos_label=1)
    sv_eer = brentq(lambda x: 1.0 - x - interp1d(fpr, tpr)(x), 0.0, 1.0)

    fpr, tpr, _ = roc_curve(spf_labels, spf_preds, pos_label=1)
    spf_eer = brentq(lambda x: 1.0 - x - interp1d(fpr, tpr)(x), 0.0, 1.0)

    return sasv_eer, sv_eer, spf_eer


text:
'''
---------------------------------------------------------------------------

    Copyright 2013    Junchen Feng

    This file is part of the Generalized Treatment Effect Toolbox.

    The Generalized Treatment Effect Toolbox is free software: you can redistribute it
    and/or modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.

---------------------------------------------------------------------------

 '''

import numpy as np

def kernelInt(x):
    '''
    NEED error check here
    '''
    value = np.array(abs(x)<=1, dtype = int)
    return value

def tricube(x,x0,h):
    z = (x-x0)/h
    kw = np.array(70./81.*(1-abs(z)**3)**3, dtype = float)*kernelInt(z);
    return kw


text:
import numpy as np
import random
import matplotlib.pyplot as plt
from functools import reduce

def sigmoid(z):
    return 1.0/(1.0+np.exp(-z))

# Função para retornar as derivadas da função Sigmóide
def sigmoid_prime(z):
    return sigmoid(z)*(1-sigmoid(z))

class Network(object):
    def __init__(self, sizes):
        self.__num_layers = len(sizes)
        self.__sizes = sizes
        self.__biases = [np.random.randn(y, 1) for y in sizes[1:]]
        self.__weights = [np.random.randn(y,x) for x,y in zip(sizes[:-1], sizes[1:])]

    def feedforward(self, a):
        neurons = []
        for b,w in zip(self.biases, self.weights):
            a = sigmoid(np.dot(w, a)+b)
            neurons.append(a)

        return neurons

    def SGB(self, training_data, epochs, eta):
        training_data = list(training_data)
        correct_data = training_data[1]
        input_data = training_data[0]
        n = len(input_data)

        if(n != len(correct_data)):
            print("Array malformatado")
            return

        erros = []
        for count in range(0, epochs):
            aux_error = []
            for inputs, correct in zip(input_data, correct_data):
                self.update_mini_batch(inputs, correct, aux_error, eta)

            erros.append(reduce(lambda x,y : [np.sum(w+z/len(input_data)) for w,z in zip(x,y)], aux_error))

        self.plotResults(epochs, erros)

    def plotResults(self, epochs, erros):
        fig, ax = plt.subplots()
        ax.plot(np.arange(epochs), erros, 'r')
        ax.set_xlabel('Iterações')
        ax.set_ylabel('Custo')
        ax.set_title('MSE vs. Epoch')
        plt.show()

    def update_mini_batch(self, inputs, correct, error, eta):
        nabla_w, nabla_b = self.backprop(inputs, correct, error)
        new_weights = []
        new_biases = []

        for delta_w, delta_b, w, b in zip(nabla_w[::-1], nabla_b[::-1], self.weights, self.biases):
            new_weights.append(w-delta_w.T*eta)
            new_biases.append(b-delta_b*eta) #Removi a transposta talvez de merda
        self.__biases = new_biases
        self.__weights = new_weights

    def backprop(self, inputs, correct, error):
        inputs = np.array([inputs]).T
        correct = np.array([correct]).T

        # print("correct ", correct)
        neurons = self.feedforward(inputs)
        result = neurons[-1]

        neurons.pop(-1)
        neurons.insert(0,inputs)

        nabla_w = []
        nabla_b = []

        error.append(self.cost(result, correct))
        aux = self.cost_derivation(result, correct) * self.function_prime(result)

        for n, w, b in zip(neurons[::-1], self.weights[::-1], self.__biases[::-1]):
            # Atualiza os bias
            nabla_b.append(aux)
            aux = np.dot(n, aux.T)

            # Atualiza os pesos
            nabla_w.append(aux)

            aux = aux * w.T
            aux = np.array([aux.sum(axis=1)]).T
            aux = aux * self.function_prime(n)
        return nabla_w, nabla_b

    # FUNÇÃO DE CUSTO A SER UTILIZADA
    def cost(self, result, correct):
        difference = result - correct
        return list(map(lambda x: np.square(x) , difference))

    # DERIVADA DA FUNÇÃO DE CUSTO
    def cost_derivation(self, result, correct):
        return result-correct

    # DERIVADA DA FUNÇÃO DE ATIVAÇÃO
    def function_prime(self, g_x):
        return g_x * (1 - g_x)

    @property
    def sizes(self):
        return self.__sizes
    @property
    def biases(self):
        return self.__biases
    @property
    def num_layers(self):
        return self.__num_layers
    @property
    def weights(self):
        return self.__weights

net = Network([4,10,2])
net.SGB([[[0,0,0,0],[0,1,0,0], [1,0,0,0], [1,1,0,0]], [[1,1], [0,0], [0,0], [1,1]]], 5000, 0.5)
# net.SGB([[[0],[1]], [[1,1], [0,0]]], 5000, 0.5)


text:
"""
Class to facilitate edge analysis as measured by # of microwells from the active array perimeter.
For example, studying edge effects related to poor loading or flowcell attach glue issues.
Leveraged in edgeEffects, spatialRL, and chipDiagnostics plugins.
"""

from scipy import ndimage
import numpy as np

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

# Bring in chipcal in case we need it for the initial mask.
try:
    import chipcal
except ImportError:
    try:
        from tools import chipcal
    except ImportError:
        print( 'Did not import chipcal in tools.edge_analyzer' )

# for 510/520/530 and 540/541, we may want to add in 50, 150, and 250 wells.
RINGS               = [0,50,100,150,200,250,300,400,500,1000,1500,2000,2500,5000,7500,10000]
IMAGE_RINGS         = [0,2,4,6,8,10,13,16,32,50] # each superpixel is ~31px for GX5v2
SMALL_IMAGE_RINGS   = [0,1,2,3,4,5,6,7,8,10,13,16,32,50]
RING        = 'ring'
RECTANGLE   = 'rectangle'

class EdgeAnalyzer:
    """
    Class for edge analysis of chip data.  Works for both full flowcell chips and multilane chips.
    Often requires chip calibration data (gain) in order to determine mask of active pixels.
    """
    def __init__( self , start_mask , is_multilane=False, rings=RINGS ):
        # pass in RINGS to make it a changeable attribute in the class instance
        self.RINGS = rings
        self.start_mask = start_mask
        self.is_multilane = is_multilane
        self.get_smaller_dim( )

    def iter_rings( self ):
        """ Helper code to iterate through all the possible 'rings' of pixels for this particular chip size."""
        strings = [ str(x) for x in self.chip_rings ] + ['center']
        return enumerate( strings )

    def get_smaller_dim( self ):
        """ returns the size of the smallest dimension, usually rows . . . """
        if self.is_multilane:
            mask = self.start_mask[:,::4]
        else:
            mask = self.start_mask
        min_dim  = np.argmin( mask.shape )
        min_size = mask.shape[ min_dim ]
        self.min_size = min_size
        return None

    def parse_chip( self, mask_shape=RING, minimum_width=1000 ):
        """ Creates a series of masks all at once for edge to center analysis. """
        #rings      = [0,100,200,300,400,500,1000,1500,2000,2500,5000,7500]
        chip_rings = []
        for i,r in enumerate(self.RINGS):
            minimum_width = minimum_width
            if (self.min_size - 2*r) < minimum_width:
                chip_rings = self.RINGS[:i]
                break
            else:
                chip_rings = self.RINGS
        # Create integer mask array
        ring_mask = np.zeros( self.start_mask.shape , np.int8 )
        if mask_shape==RECTANGLE:
            for i,diff in enumerate( chip_rings[1:] ):
                if i == 0:
                    new_mask = self.start_mask
                    initial = True
                else:
                    initial = False

                new_mask , edge = self.get_rectangle_edge_mask( new_mask , diff, initial=initial )

                ring_mask[ edge ] = (i+1)
        else:
            for i,diff in enumerate( np.diff(chip_rings) ):
                if i == 0:
                    # Start with a filled in active --> no holes from pinned
                    filled_new_mask = ndimage.morphology.binary_fill_holes( self.start_mask )

                # erode the filled in active --> symmetric masks with no holes
                filled_new_mask , filled_edge = self.get_edge_mask( filled_new_mask , diff )

                # AND with filled and start_mask --> put holes back in
                new_mask    = np.logical_and( filled_new_mask, self.start_mask )
                edge        = np.logical_and( filled_edge, self.start_mask )

                # proceed as usual
                ring_mask[ edge ] = (i+1)

        # At the end of the loop, the new center needs to be added as well
        ring_mask[ new_mask ] = (i+2)
        self.chip_rings       = chip_rings
        self.ring_mask        = ring_mask
        self.ring_pixel_count = [ (ring_mask == i).sum() for i in range( len(chip_rings) + 1 ) ]

        # Create xlabels
        self.xlabels = [ str(y) for y in self.chip_rings ] + ['center']

        return None

    def make_ringplot( self, data, ylabel=None, ylim=None, imgpath=None, operation=np.mean, downsample=False ):
        """
        Creates a visualization of data as a function of 'rings' from the edge of the active array.
        Different operations can be applied using the operation kwarg, such as np.std.
        Downsampling is available for the largest of chiptypes, where this image creation can take a bit of time.
        """
        vals = []
        x    = self.chip_rings[:-1] + np.diff( self.chip_rings ) / 2.
        for idx in range( 1 , len(x)+2 ):
            if downsample:
                vals.append( operation( data[ self.ring_mask==idx ][::10] ) )
            else:
                vals.append( operation( data[ self.ring_mask==idx ] ) )
        plt.figure(figsize=(12,4))
        xax = np.append( x , np.mean([self.chip_rings[-1],self.min_size/2]) )
        plt.plot( xax , vals , 'o-' )
        plt.xticks( self.chip_rings + [self.min_size/2] , self.xlabels , rotation=90 , fontsize=8 )

        if ylabel:
            plt.ylabel( ylabel )
        if ylim:
            plt.ylim( ylim )

        plt.xlabel( 'Pixels from Glue Line' )
        plt.tight_layout( )
        plt.grid()
        if imgpath is None:
            plt.show( )
        else:
            plt.savefig( imgpath )
        plt.close  ( )
        return (xax , vals)

    def plot_rings( self , imgpath=None ):
        """ Makes a spatial map of the rings of pixels used.  """
        data = self.ring_mask

        # Get array limits and prep for x,yticks
        r,c  = data.shape
        xdivs , ydivs = np.arange( 0 , (c+1) , c/4 ) , np.arange( 0 , (r+1) , r/4 )
        # Max plottable image is 600x800.  Downsample everything else
        if data.shape[0] > 600:
            yscale = int( data.shape[0]/600 )
        else:
            yscale = 1
        if data.shape[1] > 800:
            xscale = int( data.shape[1]/800 )
        else:
            xscale = 1
        extent = [ 0, data.shape[1], 0, data.shape[0] ]  # This might technically clip the outter pixels

        plt.figure ( )
        plt.imshow ( self.ring_mask[::yscale,::xscale] , origin='lower' , interpolation='nearest' , extent=extent )
        plt.title  ( 'Spatial Map of Edge Pixel Masks' )
        plt.xticks ( xdivs , [ str(x) for x in xdivs ] )
        plt.yticks ( ydivs , [ str(y) for y in ydivs ] )
        if imgpath is None:
            plt.show()
        else:
            plt.savefig( imgpath )
        plt.close()
        return None

    def get_rectangle_edge_mask( self, mask, pixels, initial=False ):
        # Generate a rectangular array
        if initial:
            # if this attribute doesn't exist, it's the first time through
            # --> generate start_edge_left and start_edge_right
            test = np.array( mask ).astype( np.int )
            test_mean = np.mean( test, axis=0 )
            # only look for edges in the outer 1/8ths of the array
            cutoff_edge = np.int( np.float(test_mean.shape[0])/8. )
            for i, x in enumerate( test_mean ):
                if x>0 and i<cutoff_edge:
                    self.start_edge_left = i
                    break
                elif i>=cutoff_edge:
                    self.start_edge_left=None
                    break
            for i, x in enumerate( np.flipud(test_mean) ):
                if x>0 and i<cutoff_edge:
                    self.start_edge_right = test_mean.shape[0] - i
                    break
                elif i>=cutoff_edge:
                    self.start_edge_right=None
            # get rid of inlet/outlet
            trim = np.int( np.float( test.shape[0] )/8. )
            temp = np.zeros( mask.shape ).astype( np.bool )
            temp[trim:-trim] = mask[trim:-trim]
            mask = temp
        center = np.zeros( mask.shape ).astype( np.bool )

        if self.start_edge_left:    left_lim = self.start_edge_left + pixels
        else:                       left_lim = 0

        if self.start_edge_right:   right_lim = self.start_edge_right - pixels
        else:                       right_lim = mask.shape[1]-1

        center[:, left_lim:right_lim] = mask[:, left_lim:right_lim]
        edge   = np.logical_xor( mask, center )

        return center, edge

    @classmethod
    def from_chip_gain( cls , chipcal_path , chiptype ):
        """ Returns an EdgeAnalyzer class based on chip calibration. """
        cc = chipcal.ChipCal( chipcal_path , chiptype )
        cc.load_gain     ( )
        cc.find_refpix   ( )
        cc.determine_lane( )

        edge = cls( cc.active , cc.is_multilane )
        return edge

    @staticmethod
    def get_edge_mask( mask , pixels ):
        """ Creates a new edge mask from mask from <pixels> pixels from the edge of the array. """
        center = ndimage.morphology.binary_erosion( mask , iterations=pixels )
        edge   = np.logical_xor( mask, center ) # was [edge = mask - center] but bool '-' is now deprecated in np
        return center, edge

max_stars_repo_name: iontorrent/TS

max_issues_repo_name: iontorrent/TS

max_forks_repo_name: iontorrent/TS


text:
# Copyright (c) OpenMMLab. All rights reserved.
import os
import os.path as osp
from typing import List, Optional, Sequence, Union

import mmcv
import numpy as np
from mmcls.datasets.builder import DATASETS
from typing_extensions import Literal

from .base import BaseFewShotDataset

TRAIN_CLASSES = [
    'n02074367', 'n03047690', 'n03854065', 'n02089867', 'n02105505',
    'n01704323', 'n04604644', 'n03676483', 'n01558993', 'n07697537',
    'n04509417', 'n02101006', 'n02165456', 'n13133613', 'n02747177',
    'n02966193', 'n03924679', 'n04275548', 'n02113712', 'n03838899',
    'n02091831', 'n03220513', 'n07747607', 'n03998194', 'n02108089',
    'n09246464', 'n04251144', 'n02111277', 'n04435653', 'n03207743',
    'n04389033', 'n03337140', 'n03908618', 'n02606052', 'n01770081',
    'n01910747', 'n03062245', 'n02108551', 'n03017168', 'n04258138',
    'n03888605', 'n04596742', 'n07584110', 'n02687172', 'n03476684',
    'n04243546', 'n02795169', 'n02457408', 'n04515003', 'n06794110',
    'n01532829', 'n01843383', 'n13054560', 'n04067472', 'n03347037',
    'n04612504', 'n03400231', 'n01749939', 'n02823428', 'n04296562',
    'n03527444', 'n04443257', 'n02108915', 'n02120079'
]
VAL_CLASSES = [
    'n02138441', 'n02981792', 'n02174001', 'n03535780', 'n03770439',
    'n03773504', 'n02950826', 'n03980874', 'n02114548', 'n03584254',
    'n02091244', 'n03417042', 'n02971356', 'n01855672', 'n09256479',
    'n03075370'
]
TEST_CLASSES = [
    'n02110341', 'n01981276', 'n07613480', 'n02129165', 'n04418357',
    'n02443484', 'n03127925', 'n01930112', 'n03272010', 'n03146219',
    'n04146614', 'n03775546', 'n04522168', 'n02099601', 'n02871525',
    'n02110063', 'n02219486', 'n02116738', 'n04149813', 'n03544143'
]

@DATASETS.register_module()
class MiniImageNetDataset(BaseFewShotDataset):
    """MiniImageNet dataset for few shot classification.

    Args:
        subset (str| list[str]): The classes of whole dataset are split into
            three disjoint subset: train, val and test. If subset is a string,
            only one subset data will be loaded. If subset is a list of
            string, then all data of subset in list will be loaded.
            Options: ['train', 'val', 'test']. Default: 'train'.
        file_format (str): The file format of the image. Default: 'JPEG'
    """

    resource = 'https://github.com/twitter/meta-learning-lstm/tree/master/data/miniImagenet'  # noqa

    TRAIN_CLASSES = TRAIN_CLASSES
    VAL_CLASSES = VAL_CLASSES
    TEST_CLASSES = TEST_CLASSES

    def __init__(self,
                 subset: Literal['train', 'test', 'val'] = 'train',
                 file_format: str = 'JPEG',
                 *args,
                 **kwargs):
        if isinstance(subset, str):
            subset = [subset]
        for subset_ in subset:
            assert subset_ in ['train', 'test', 'val']
        self.subset = subset
        self.file_format = file_format
        super().__init__(*args, **kwargs)

    def get_classes(
            self,
            classes: Optional[Union[Sequence[str],
                                    str]] = None) -> Sequence[str]:
        """Get class names of current dataset.

        Args:
            classes (Sequence[str] | str | None): Three types of input
                will correspond to different processing logics:

                - If `classes` is a tuple or list, it will override the
                  CLASSES predefined in the dataset.
                - If `classes` is None, we directly use pre-defined CLASSES
                  will be used by the dataset.
                - If `classes` is a string, it is the path of a classes file
                  that contains the name of all classes. Each line of the file
                  contains a single class name.

        Returns:
            tuple[str] or list[str]: Names of categories of the dataset.
        """
        if classes is None:
            class_names = []
            for subset_ in self.subset:
                if subset_ == 'train':
                    class_names += self.TRAIN_CLASSES
                elif subset_ == 'val':
                    class_names += self.VAL_CLASSES
                elif subset_ == 'test':
                    class_names += self.TEST_CLASSES
                else:
                    raise ValueError(f'invalid subset {subset_} only '
                                     f'support train, val or test.')
        elif isinstance(classes, str):
            # take it as a file path
            class_names = mmcv.list_from_file(classes)
        elif isinstance(classes, (tuple, list)):
            class_names = classes
        else:
            raise ValueError(f'Unsupported type {type(classes)} of classes.')
        return class_names

    def load_annotations(self) -> List:
        """Load annotation according to the classes subset."""
        img_file_list = {
            class_name: sorted(
                os.listdir(osp.join(self.data_prefix, 'images', class_name)),
                key=lambda x: int(x.split('.')[0].split('_')[-1]))
            for class_name in self.CLASSES
        }
        data_infos = []
        for subset_ in self.subset:
            ann_file = osp.join(self.data_prefix, f'{subset_}.csv')
            assert osp.exists(ann_file), \
                f'Please download ann_file through {self.resource}.'
            with open(ann_file) as f:
                for i, line in enumerate(f):
                    # skip file head
                    if i == 0:
                        continue
                    filename, class_name = line.strip().split(',')
                    filename = img_file_list[class_name][
                        int(filename.split('.')[0][-5:]) - 1]
                    gt_label = self.class_to_idx[class_name]
                    info = {
                        'img_prefix':
                        osp.join(self.data_prefix, 'images', class_name),
                        'img_info': {
                            'filename': filename
                        },
                        'gt_label':
                        np.array(gt_label, dtype=np.int64)
                    }
                    data_infos.append(info)
        return data_infos

max_stars_repo_name: BIGWangYuDong/mmfewshot

max_issues_repo_name: BIGWangYuDong/mmfewshot

max_forks_repo_name: BIGWangYuDong/mmfewshot


text:
from typing import List, Tuple

import nltk
import numpy as np

# Spacy needs this module, but it's used only implicitly
import pyinflect  # noqa: F401
import spacy
from gender_extractor import GenderExtractor

from initialize import spacy_nlp
from interfaces.QuestionAnswerOperation import QuestionAnswerOperation
from tasks.TaskTypes import TaskType

"""
Base Class for implementing the different input transformations a generation should be robust against.
"""

class SuspectingParaphraser(QuestionAnswerOperation):
    """This paraphraser transforms a yes/no question into a tag one.

    Example: "Did the American National Shipment company really break its own fleet?"
    -> "The American National Shipment company really broke its own fleet, didn't it?"
    """

    tasks = [TaskType.QUESTION_ANSWERING, TaskType.QUESTION_GENERATION]

    languages = ["en"]

    def __init__(self, seed=0, max_outputs=1, pronoun_mod=0.9):
        super().__init__(seed, max_outputs=max_outputs)
        np.random.seed(seed)
        nltk.download("punkt")

        self.nlp = spacy_nlp if spacy_nlp else spacy.load("en_core_web_sm")

        self.gender_detector = GenderExtractor()
        self.pronouns = ["he", "she", "it", "they"]
        self.static_pronouns = ["i", "we", "you", *self.pronouns]

        self._special_endings = {
            "may": "may {} not",
            "might": "might {} not",
            "shall": "shan't {}",
            "will": "won't {}",
        }

        self.pronoun_mod = pronoun_mod
        self._pronoun_alt = (1 - pronoun_mod) / (len(self.pronouns) - 1)

    def _transform(self, question):
        text = nltk.word_tokenize(question)
        doc = self.nlp(question)
        modal = str(doc[0]).lower()
        token = doc[0]

        verb_position = [
            i for i in range(len(doc)) if str(doc[i]) == token.head.text
        ][0]

        rest_of_sentence = [i for i in text[verb_position:]]

        if text[1].lower() == "n't":
            text = text[1:]
            doc = doc[1:]
            verb_position = verb_position - 1

        beginning = [text[1].capitalize()]
        beginning.extend(text[2:verb_position])
        sentence = nltk.tokenize.treebank.TreebankWordDetokenizer().detokenize(
            beginning + rest_of_sentence
        )

        first_verb = doc[verb_position]

        # If 'did' is our modal, the verb will be in a present tense
        # It means that we need to inflect it to the past one (VBD)
        # (Did John _drink_ my tea? -> John _drank_ my tea, didn't he?)
        # Otherwise, the verb is already in a good form and we can use
        # it directly
        if modal == "did":
            demodded = first_verb._.inflect("VBD")
        else:
            demodded = modal + " " + str(first_verb)
        sentence = sentence.replace(str(first_verb), str(demodded)).replace(
            "?", ""
        )

        ending = self._resolve_ending(doc, modal)
        result = sentence + ending
        return result

    def _resolve_ending(self, doc, modal):
        try:
            subject = str([tok for tok in doc if (tok.dep_ == "nsubj")][0])
        except IndexError:
            return ", right?"

        prob = {i: 1 / len(self.pronouns) for i in self.pronouns}

        tagged = [(X.text, X.label_) for X in doc.ents]
        if subject.lower() in self.static_pronouns:
            pronoun = subject.lower()
            if pronoun == "i":
                pronoun = "I"
        else:
            if len(tagged) > 0 and tagged[0][1] != "PERSON":
                prob = {i: 0 for i in self.pronouns}
                prob["it"] = 1
            else:
                noun_gender = self.gender_detector.extract_gender(subject)

                if noun_gender in ["male", "mostly_male"]:
                    prob = {i: self._pronoun_alt for i in self.pronouns}

                    prob["he"] = self.pronoun_mod

                elif noun_gender in ["female", "mostly_female"]:
                    prob = {i: self._pronoun_alt for i in self.pronouns}

                    prob["she"] = self.pronoun_mod

            pronoun = np.random.choice(self.pronouns, p=list(prob.values()))

        ending = ", "
        if modal in self._special_endings.keys():
            ending += self._special_endings[modal].format(pronoun)
        else:
            ending += f"{modal}n't {pronoun}"
        ending += "?"
        return ending

    def _filter_phrase(self, question):
        try:
            if question.strip()[-1] != "?":
                return False
        except IndexError:
            return False

        if " or " in question:
            return False

        doc = self.nlp(question)
        token = doc[0]
        if token.pos_ != "AUX":
            return False

        return True

    def generate(
        self, context: str, question: str, answers: [str]
    ) -> List[Tuple[str, str, List[str]]]:
        if not self._filter_phrase(question):
            return [(context, question, answers)]

        paraphrased = self._transform(question)
        return [(context, paraphrased, answers)]

if __name__ == "__main__":
    import json

    from TestRunner import convert_to_snake_case

    tf = SuspectingParaphraser()

    test_cases = []
    for i, sentence in enumerate(
        [
            "Did Sally finally return the french book to Chris?",
            "Did the American National Shipment company really break its own fleet?",
            "Couldn't she just leave?",
            "Shall you begone, lad?",
            "Has Buzz Aldrin, the first person who walked on the moon, brought back some aliens?",
        ]
    ):
        res = tf.generate("", sentence, [])
        test_cases.append(
            {
                "class": tf.name(),
                "inputs": {"context": "", "question": sentence, "answers": []},
                "outputs": [],
            }
        )

        for p_context, p_question, p_answers in res:
            print(sentence)
            print(p_question)
            print()
            test_cases[i]["outputs"].append(
                {
                    "context": p_context,
                    "question": p_question,
                    "answers": p_answers,
                }
            )

    json_file = {
        "type": convert_to_snake_case(tf.name()),
        "test_cases": test_cases,
    }

    with open("test.json", "w") as f:
        json.dump(json_file, f, indent=2)


text:
# Cell cycle classification and feature selection - perform feature selection and prediction with internal and external cross-validation; results are saved as figures and in a hdf5 file.
#

########
######## PACKAGE LOADING
########
from __future__ import print_function
import h5py
import sys
import os
from os.path import exists
sys.path.append('./')
import scipy as SP
from numpy import inf
import pdb
import matplotlib as mpl
if 0:
	mpl.use('Agg')
import matplotlib.pyplot as plt
import pylab as PL
from sklearn.decomposition import PCA

from sklearn.naive_bayes import GaussianNB
from sklearn import metrics, svm, ensemble, linear_model, preprocessing
from sklearn.grid_search import GridSearchCV
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.svm import SVC, l1_min_c
from sklearn.feature_selection import RFECV, SelectKBest

from sklearn import metrics, ensemble
from sklearn.cross_validation import StratifiedKFold, LeaveOneOut
from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
from sklearn.datasets import make_classification
import warnings
warnings.filterwarnings('ignore')
# hacky hack we need for Windows users
#os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'

class cyclone:
	"""
	cyclone
	This class takes care of assigning cell cycle stage based on cell cycle annotated genes in the  transcriptome.
	"""
	def __init__(self,Y,row_namesY = None,cc_geneNames = None,labels = None, Y_tst = None, row_namesY_tst = None, labels_tst = None, learn_reverse = False, norm = 'rank'):
		print("Initialise model...")
		assert row_namesY!=None, 'cyclone: provide gene names for Y'
		assert cc_geneNames!=None, 'cyclone: provide cell cycle genes'
		assert labels!=None, 'cyclone: provide labels for training'
		assert norm in ['rank', 'tc', 'median'] , 'normalisation has to be either "rank" (rank normalisation) or "tc" (total count) or "median" (log2 fold change to median)'

		cc_ens = cc_geneNames
		genes = row_namesY
		genesT = row_namesY_tst
		ind_cc_tr = SP.zeros((len(cc_ens),))
		if Y_tst!=None and row_namesY_tst!=None:
			ind_cc_tr = SP.zeros((len(cc_ens),))
			ind_cc_ts = SP.zeros((len(cc_ens),))
			for i in range(len(cc_ens)):
				ind_cc_ts[i] = SP.where(map(lambda x:x==cc_ens[i], genesT))[0][0]
				ind_cc_tr[i] = SP.where(map(lambda x:x==cc_ens[i], genes))[0][0]
			inds_tr = ind_cc_tr.astype('int32')
			inds_ts = ind_cc_ts.astype('int32')
			Y_tst = Y_tst[:,inds_ts]
			Y = Y[:,inds_tr]
			if norm == 'rank':
				for ir in range(Y_tst.shape[0]):
					Y_tst[ir,:] = SP.stats.rankdata(Y_tst[ir,:], method='average')
				for ir in range(Y.shape[0]):
					Y[ir,:] = SP.stats.rankdata(Y[ir,:], method='average')
			elif norm=='tc':
		   		Y_tst = SP.transpose(SP.transpose(Y_tst)/SP.sum(Y_tst,1))
				Y = SP.transpose(SP.transpose(Y)/SP.sum(Y,1))
			else:
				for ir in range(Y_tst.shape[0]):
					med_ = SP.median(Y_tst[ir,:])
					Y_tst[ir,:] = SP.log2(Y_tst[ir,:]/med_)
				for ir in range(Y.shape[0]):
					med_ = SP.median(Y[ir,:])
					Y[ir,:] = SP.log2(Y[ir,:]/med_)
				Y[Y==-inf] = min(Y[Y>-inf])-1
				Y_tst[Y_tst==-inf] = min(Y_tst[Y_tst>-inf])-1
			if learn_reverse == True:
				labels_ = labels_tst
				Y_ = Y_tst
				Y_tst = Y
				labels_tst = labels
				Y = Y_
				labels = labels_
			self.Y = Y
			self.Y_tst = Y_tst
			self.scores = None
			self.labels = labels
			self.labels_tst = labels_tst
			self.numClasses_tst = len(SP.unique((labels_tst)))
			self.inds_tr = inds_tr
			self.inds_tst = inds_ts
		else:
			ind_cc_tr = SP.zeros((len(cc_ens),))
			for i in range(len(cc_ens)):
				ind_cc_tr[i] = SP.where(map(lambda x:x==cc_ens[i], cc_genes))[0][0]
			inds_tr = ind_cc_tr.astype('int32')
			for ir in range(Y.shape[0]):
				Y[ir,:] = SP.stats.rankdata(Y[ir,:])
			self.Y = Y[:,inds_tr]
			slef.Y_tst=None

		self.geneNames = cc_geneNames
		self.numClasses = max(labels)#+1

	def trainModel(self, do_pca = False,out_dir='./cache', rftop = 40, class_labels = SP.array(['G1','S','G2M']), cv=10, npc=3, is_SVM=1, is_RFE=0 , scale=False):
		if not os.path.exists(out_dir):
			os.makedirs(out_dir)
		CFG = {}
		CFG['is_RFE'] = is_RFE # use recursive feature selection (can be slow for large datasets)
		CFG['is_SVM'] = is_SVM # use SVM with univariate feature selection (faster than RFE)
		CFG['CV_inner'] = cv #inner CV for RFE_CV: either an int or 'LOOCV'
		CFG['out_dir'] = out_dir
		CFG['do_pca'] = do_pca
		CFG['lassotop'] = 20
		self.cv = cv
		Y = self.Y
		labels = self.labels
		var_names = self.geneNames
		numClasses = self.numClasses
		predRF = SP.zeros((len(labels),numClasses))
		predSVM = SP.zeros((len(labels),numClasses))
		predSVMrbf = SP.zeros((len(labels),numClasses))
		predGNB = SP.zeros((len(labels),numClasses))
		predLR = SP.zeros((len(labels),numClasses))
		predLRall = SP.zeros((len(labels),numClasses))
		names_dict={}
		if self.cv == 'LOOCV':
			loo = LeaveOneOut(len(labels))
			CV_list = (list(iter(loo)))
			CV_list.append((SP.array(range(Y.shape[0])), SP.array(range(Y.shape[0]))))#all data...
		else:
			skf = StratifiedKFold(labels, n_folds=self.cv)
			CV_list = (list(iter(skf)))
			CV_list.append((SP.array(range(Y.shape[0])), SP.array(range(Y.shape[0]))))#all data...
		lambda_best = SP.zeros((1,len(CV_list))).ravel()
		print("Performing cross validation ...")
		for i in range(len(CV_list)):
			if i<len(CV_list)-1:
				print("Fold " + str(i+1) + " of " + str(len(CV_list)-1))
			else:
				print("Final model")
			# string label for this fold
			#get data of a CV run
			cv_tr = CV_list[i][0]
			cv_tst = CV_list[i][1]
			lab_tr = labels[cv_tr]
			Ytr = Y[cv_tr,:]
			Ytst = Y[cv_tst,:]
			lab_tst = labels[cv_tst]
			if (i==len(CV_list)-1):
				foldlabel = 'full'
				if (self.Y_tst==None):
					Ytst = Y[cv_tst,:]
					lab_tst = labels[cv_tst]
				else:
					foldlabel = 'Test'
					Ytst = self.Y_tst
					lab_tst = self.labels_tst
			else:
				foldlabel = str(i)
			if do_pca>=1:
				npc = npc#3
				#do PCA to get features
				pcaCC = PCA(n_components=npc, whiten=False)
				pcaCC.fit(Ytr)
				pcaTst=pcaCC.transform(Ytst)
				pcaTr=pcaCC.transform(Ytr)
				#selection = SelectKBest(k=1)
				#combined_features = FeatureUnion([("pca", pcaCC), ("univ_select", selection)])
				combined_features = FeatureUnion([("pca", pcaCC)])
				gnb = GaussianNB()
				y_pred = gnb.fit(pcaTr, lab_tr).predict_proba(pcaTst)
				if i<len(CV_list)-1:
					predGNB[cv_tst,:] =y_pred#[:,1]
				else:
					predGNB_ts = y_pred#[:,1]
			if do_pca==2:
				Ytr = SP.concatenate((Ytr, pcaTr),1)
				Ytst = SP.concatenate((Ytst, pcaTst),1)
				pcnames = []
				for pci in range(npc):
					pcnames.append('PC'+str(pci+1))
				var_names = SP.concatenate((var_names, SP.array(pcnames)),1)
			print("  Computing random forest ...")

			if CFG['is_RFE']==1:#Recursive feature selection with SVM
				print("  Computing RFE with SVM ...")
				svc = SVC(kernel="linear", probability=False, class_weight='auto')#use linear SVM for selection
				rfecv = RFECV(estimator=svc, step=1,scoring='f1')
				param_grid = dict(estimator__C=[0.1, 1, 10, 100, 1000])
				clf_rfe = GridSearchCV(rfecv, param_grid=param_grid, cv=3, scoring='f1')#GridSearch to find optimal parameters
				clf_rfe.fit(Ytr, lab_tr)
				svc = SVC(kernel="linear", probability=False,C=clf_rfe.best_estimator_.estimator.C, class_weight='auto')#use linear SVM for selection
				if CFG['CV_inner']=='':
					rfecv = RFECV(estimator=svc, step=1,scoring='f1')
				elif CFG['CV_inner']=='LOOCV':
					rfecv = RFECV(estimator=svc, step=1,scoring='f1', cv=LeaveOneOut(len(lab_tr)))
				else:
					rfecv = RFECV(estimator=svc, step=1,scoring='f1', cv=StratifiedKFold(lab_tr, n_folds=CFG['CV_inner']))
				clf_rfe.best_estimator_.fit(Ytr, lab_tr)
				predicted = clf_rfe.best_estimator_.predict(Ytst)
				if i<len(CV_list)-1:
					predSVM[cv_tst,:] = predicted
				else:
					predSVM_ts[cv_tst] = predicted
				classifier = svm.SVC(kernel='rbf', gamma=0.05, class_weight='auto', probability=True)#rbf kernel for prediction
				param_grid = dict(C=[0.1, 1], gamma=[1e-1,1e-2,1e-3])
				clf_rbf = GridSearchCV(classifier, param_grid=param_grid, cv=3, scoring='f1')
				clf_rbf.fit(Ytr[:,clf_rfe.best_estimator_.ranking_==1], lab_tr)
				clf_rbf.best_estimator_.fit(Ytr[:,clf_rfe.best_estimator_.ranking_==1], lab_tr)
				predicted = clf_rbf.best_estimator_.predict_proba(Ytst[:,clf_rfe.best_estimator_.ranking_==1])
				if i<len(CV_list)-1:
					predSVMrbf[cv_tst,:] = predicted
				fpr, tpr, thresholds = metrics.roc_curve(lab_tst, predicted[:,1])
				if (i==len(CV_list)-1) | CFG["CV_plots"]>0:
					PL.figure()
					PL.plot(fpr, tpr)
					PL.savefig(CFG['out_dir']+'/RF_SVM_'+foldlabel+'.pdf')
					names_dict[foldlabel+'_SVM']=self.geneNames[clf_rfe.best_estimator_.ranking_==1]
			elif CFG['is_SVM']==1:#univariate FS with rbf SVM; choose this if you hava a large data set (many features, eg RNAseq)
				print("  SVM feature selection ...")
				classifier = svm.SVC(kernel='rbf', gamma=0.05, class_weight='auto', probability=True)
				selection = SelectKBest(k=1)
				combined_features = FeatureUnion([("univ_select", selection)])

				X_features = combined_features.fit(Ytr, lab_tr).transform(Ytr)
				scaler = preprocessing.StandardScaler().fit(Ytr)
				YtrS = scaler.transform(Ytr)
				YtstS = scaler.transform(Ytst)

				classifier.fit(X_features, lab_tr)
				pipeline = Pipeline([("features", combined_features), ("svm", classifier)])
				if CFG['do_pca']==3:
					param_grid = dict(features__pca__n_components=SP.unique(SP.round_(SP.logspace(1.0,max(SP.log2(Ytr.shape[1]), SP.log2(10)),num=min(5,Ytr.shape[1]),base=2.0))),
									  features__univ_select__k=SP.unique(SP.round_(SP.logspace(3.0,SP.log2(Ytr.shape[1]),num=min(10,Ytr.shape[1]),base=2.0))),
									  svm__C=[0.1, 1, 10], svm__gamma=[1e-1,1e-2,1e-3])
				else:
					C_range = 10. ** SP.arange(0, 2)
					gamma_range = 10. ** SP.arange(-5, 1)
					param_grid = dict(features__univ_select__k=SP.unique(SP.round_(SP.logspace(3.0,SP.log2(Ytr.shape[1]),num=min(10,Ytr.shape[1]),base=2.0))),
									  svm__C=C_range, svm__gamma=gamma_range)
				clf = GridSearchCV(pipeline, param_grid=param_grid, cv=5, scoring='f1')
				clf.fit(YtrS, lab_tr)
				print("The best classifier is: ", clf.best_estimator_)
				select_best=clf.best_estimator_.get_params()['features__univ_select']
				#names_dict[foldlabel+'_SVM']=self.geneNames[SP.argsort(-1.0*select_best.scores_)[0:(select_best.k-1)]]
				expected = lab_tst
				predicted = clf.best_estimator_.predict_proba(YtstS)
				if i<len(CV_list)-1:
					predSVM[cv_tst,:] = predicted
				else:
					predSVM_ts = predicted
				#print(clf.best_estimator_)

				classifier = svm.SVC(kernel='rbf', gamma=0.05, class_weight='auto', probability=True)#rbf kernel for prediction
				param_grid = dict(C=[1,10], gamma=[ 1e-1,1e-2,1e-3])
				clf_rbf = GridSearchCV(classifier, param_grid=param_grid, cv=5, scoring='f1')
				clf_rbf.fit(Ytr, lab_tr)
				clf_rbf.best_estimator_.fit(Ytr, lab_tr)
				predicted = clf_rbf.best_estimator_.predict_proba(Ytst)
				if i<len(CV_list)-1:
					predSVMrbf[cv_tst,:] = predicted
				else:
					predSVMrbf_ts = predicted

			#do lasso with regularisation path
			cs = l1_min_c(Ytr, lab_tr, loss='log') * SP.logspace(0, 3)
			print("  Computing regularization path ...")

			lasso = linear_model.LogisticRegression(C=cs[0]*10.0, penalty='l1', tol=1e-6)
			param_grid = dict(C=cs)
			clf_lr = GridSearchCV(lasso, param_grid=param_grid, cv=5, scoring='f1')
			clf_lr.fit(Ytr, lab_tr)
			clf_lr.best_estimator_.fit(Ytr, lab_tr)
			lambda_best[i] = clf_lr.best_params_.get('C')
			predicted = clf_lr.best_estimator_.predict_proba(Ytst)

			clf = linear_model.LogisticRegression(C=cs[0]*10.0, penalty='l1', tol=1e-6)
			coefs_ = []
			for c in cs:
				clf.set_params(C=c)
				clf.fit(Ytr, lab_tr)
				coefs_.append(clf.coef_.ravel().copy())

			if i<len(CV_list)-1:
				predLR[cv_tst,:] = predicted
			else:
				predLR_ts = predicted
			coefs_ = SP.array(coefs_)
			# get ordering by importance (how many times they appear)
			order=(coefs_!=0).sum(axis=0).argsort()
			order=order[::-1] # descending
			# store this order
			featrank_lasso = order
			showtop= min(Ytr.shape[1], CFG['lassotop'])

			clfAll = linear_model.LogisticRegression(C=1e5, penalty='l2', tol=1e-6)
			clfAll.fit(Ytr, lab_tr)
			predicted = clfAll.predict_proba(Ytst)
			if i<len(CV_list)-1:
				predLRall[cv_tst,:] = predicted
			else:
				predLRall_ts = predicted
			forest = ExtraTreesClassifier(n_estimators=500,
										  random_state=0, criterion="entropy", bootstrap=False)
			#forest = RandomForestClassifier(n_estimators=500,
			#							 random_state=0, criterion="entropy")
			forest.fit(Ytr, lab_tr)
			pred = forest.predict_proba(Ytst)
			#pdb.set_trace()
			if i<len(CV_list)-1:
				predRF[cv_tst,:] = pred#[:,1]
			else:
				predRF_ts = pred#[:,1]
			importances = forest.feature_importances_
			std = SP.std([tree.feature_importances_ for tree in forest.estimators_],
						 axis=0)

			topfeat=min(Ytr.shape[1], rftop)
			indices = SP.argsort(importances)[::-1][0:topfeat]
			# store full feature ranking
			featrank_rf = SP.argsort(importances)[::-1]
			# Plot the feature importances of the forest
			if (i==len(CV_list)-1):
				PL.figure()
				#PL.title("Feature importances, Fold "+foldddPPlabel+', AUC='+str(SP.round_(metrics.auc(fpr, tpr),3)))
				PL.title("Feature importances")
				#PL.bar(range(topfeat), importances[indices],color="r", yerr=std[indices], align="center")
				PL.bar(range(topfeat), importances[indices],color="r", align="center")
				PL.xticks(range(topfeat), indices, rotation=70)
				PL.gca().set_xticklabels(var_names[indices])
				PL.setp(PL.gca().get_xticklabels(), fontsize=8)
				PL.xlim([-1, topfeat])
				PL.savefig(out_dir+'/RF_featureimportance_'+foldlabel+'.pdf')

		f2 = open(os.path.join(out_dir,'classification_reportCV.txt')  ,'w')

		predRFv = SP.argmax(predRF_ts,axis=1)+1
		predRF_trv = SP.argmax(predRF,axis=1)+1
		self.scores = predRF
		self.scores_tst = predRF_ts
		self.ranking = var_names[indices]

		predLRv = SP.argmax(predLR_ts,axis=1)+1
		predLR_trv = SP.argmax(predLR,axis=1)+1
		self.scoresLR = predLR
		self.scoresLR_tst = predLR_ts

		predLRallv = SP.argmax(predLRall_ts,axis=1)+1
		predLRall_trv = SP.argmax(predLRall,axis=1)+1
		self.scoresLRall = predLRall
		self.scoresLRall_tst = predLRall_ts
		if CFG['is_SVM']==1:
			predSVMv = SP.argmax(predSVM_ts,axis=1)+1
			predSVM_trv = SP.argmax(predSVM,axis=1)+1
			self.scoresSVM = predSVM
			self.scoresSVM_tst = predSVM_ts

			predSVMrbfv = SP.argmax(predSVMrbf_ts,axis=1)+1
			predSVMrbf_trv = SP.argmax(predSVMrbf,axis=1)+1
			self.scoresSVMrbf = predSVMrbf
			self.scoresSVMrbf_tst = predSVMrbf_ts

		predGNBv = SP.argmax(predGNB_ts,axis=1)+1
		predGNB_trv = SP.argmax(predGNB,axis=1)+1
		self.scoresGNB = predGNB
		self.scoresGNB_tst = predGNB_ts

		print("Classification report for classifier %s:\n%s\n" % ('Gaussian Naive Bayes', metrics.classification_report(self.labels, predGNB_trv)))
		print("Classification report for classifier %s:\n%s\n" % ('Random Forest', metrics.classification_report(self.labels, predRF_trv)))
		print("Classification report for classifier %s:\n%s\n" % ('LR', metrics.classification_report(self.labels, predLR_trv)))
		print("Classification report for classifier %s:\n%s\n" % ('LRall', metrics.classification_report(self.labels, predLRall_trv)))
		if CFG['is_RFE']==1:
			print("Classification report for classifier %s:\n%s\n" % ('SVM ', metrics.classification_report(labels, predSVM>0.5)),file=f2)
		elif CFG['is_SVM']==1:
			print("Classification report for classifier %s:\n%s\n" % ('SVM', metrics.classification_report(self.labels, predSVM_trv)))
			print("Classification report for classifier %s:\n%s\n" % ('SVMrbf', metrics.classification_report(self.labels, predSVMrbf_trv)))
		f2.close()

	def plotHistograms(self, class_labels = SP.array(['G1','S','G2M']),out_dir='./cache', plot_tst = True, method = 'RF',do_h=False):
		plparams = {'backend': 'pdf',
		  'axes.labelsize': 14,
		  'text.fontsize': 14,
		  'legend.fontsize': 13,
		  'xtick.labelsize': 14,
		  'ytick.labelsize': 14,
		  'text.usetex': False}
		PL.rcParams.update(plparams)
		assert self.scores != None, 'cyclone: first train the model before attempting to plot'
		if not os.path.exists(out_dir):
			os.makedirs(out_dir)
		width =0.75
		ind_b = SP.arange(3)
		if method=='RF':
			predRF = self.scores
		elif  method=='SVM':
			predRF = self.scoresSVM
		elif  method=='SVMrbf':
			predRF = self.scoresSVMrbf
		elif  method=='GNB':
			predRF = self.scoresGNB
		elif  method=='LR':
			predRF = self.scoresLR
		elif  method=='LRall':
			predRF = self.scoresLRall
		predRF_trv = SP.argmax(predRF,axis=1)+1
		labels = self.labels
		if plot_tst ==True:
			if method=='RF':
				predRF_ts = self.scores_tst
			elif method=='SVM':
				predRF_ts = self.scoresSVM_tst
			elif method=='SVMrbf':
				predRF_ts = self.scoresSVMrbf_tst
			elif method=='LR':
				predRF_ts = self.scoresLR_tst
			elif method=='LRall':
				predRF_ts = self.scoresLRall_tst
			elif method=='GNB':
				predRF_ts = self.scoresGNB_tst
			predRFv = SP.argmax(predRF_ts,axis=1)+1
			lab_tst = self.labels_tst
			u_classes = SP.unique(lab_tst)
			ind_b = SP.arange(len(u_classes))
			G1pred = SP.array([SP.sum(predRFv[lab_tst==iclass]==1) for iclass in u_classes])
			Spred = SP.array([SP.sum(predRFv[lab_tst==iclass]==2) for iclass in u_classes])
			G2Mpred = SP.array([SP.sum(predRFv[lab_tst==iclass]==3) for iclass in u_classes])

			nClass = SP.array([SP.sum(lab_tst==iclass).astype('double') for iclass in u_classes])

			G1pred_rel = SP.array([SP.sum(predRFv[lab_tst==iclass]==1)/nClass[iclass-1] for iclass in u_classes])
			Spred_rel = SP.array([SP.sum(predRFv[lab_tst==iclass]==2)/nClass[iclass-1] for iclass in u_classes])
			G2Mpred_rel = SP.array([SP.sum(predRFv[lab_tst==iclass]==3)/nClass[iclass-1] for iclass in u_classes])

			cols = ['#1b9e77', '#d95f02', '#7570b3']
			if do_h == True:
				fig_s = 2.0/3.0*len(class_labels)
				PL.figure(figsize=(5.0,fig_s))
				p1 = plt.barh(ind_b, G1pred, height = 0.9, color=cols[0])
				p2 = plt.barh(ind_b, Spred, color=cols[1], left=G1pred, height = 0.9)
				p3 = plt.barh(ind_b, G2Mpred, color=cols[2], left=G1pred+Spred, height = 0.9)
				PL.xlabel('# cells')
				PL.yticks(ind_b+width/2., class_labels )
			else:
				PL.figure(figsize=(5,5))
				p1 = plt.bar(ind_b, G1pred,   width, color=cols[0])
				p2 = plt.bar(ind_b, Spred,width, color=cols[1], bottom=G1pred)
				p3 = plt.bar(ind_b, G2Mpred,width, color=cols[2], bottom=G1pred+Spred)
				PL.ylabel('# cells')
				PL.title('True class')
				PL.xticks(ind_b+width/2., class_labels )
			#PL.yticks(SP.arange(0,20,5))
				# remove top and right axis
			ax = PL.gca()
			ax.spines["right"].set_visible(False)
			ax.spines["top"].set_visible(False)
			ax.get_xaxis().tick_bottom()
			ax.get_yaxis().tick_left()
			lgd = PL.legend( (p1[0], p2[0],p3[0]), ('Pred. G1', 'Pred. S','Pred. G2M') ,loc='center left', bbox_to_anchor=(1, 0.5))
			PL.savefig(out_dir+'/barchart_h'+str(do_h)+method+'.pdf',bbox_extra_artists=(lgd,),bbox_inches='tight')
			if do_h == True:
				fig_s = 2.0/3.0*len(class_labels)
				PL.figure(figsize=(5.0,fig_s))
				p11 = plt.barh(ind_b, G1pred_rel, color=cols[0], height = 0.9)
				p22 = plt.barh(ind_b, Spred_rel, color=cols[1], left=G1pred_rel, height = 0.9)
				p33 = plt.barh(ind_b, G2Mpred_rel, color=cols[2], left=G1pred_rel+Spred_rel, height = 0.9)
				PL.xlabel('Fraction')
				PL.yticks(ind_b+width/2., class_labels)
			else:
				PL.figure(figsize=(5,5))
				p11 = plt.bar(ind_b, G1pred_rel,   width, color=cols[0])
				p22 = plt.bar(ind_b, Spred_rel,width, color=cols[1], bottom=G1pred_rel)
				p33 = plt.bar(ind_b, G2Mpred_rel,width, color=cols[2], bottom=G1pred_rel+Spred_rel)
				PL.ylabel('Fraction')
				PL.title('True cell cycle phase')
				PL.xticks(ind_b+width/2., class_labels)
			#PL.yticks(SP.arange(0,20,5))
			ax = PL.gca()
			ax.spines["right"].set_visible(False)
			ax.spines["top"].set_visible(False)
			ax.get_xaxis().tick_bottom()
			ax.get_yaxis().tick_left()
			lgd = PL.legend( (p11[0], p22[0],p33[0]), ('Pred. G1', 'Pred. S','Pred. G2M') ,loc='center left', bbox_to_anchor=(1, 0.5))
			PL.savefig(out_dir+'/barchart_rel_h'+str(do_h)+method+'.pdf',bbox_extra_artists=(lgd,),bbox_inches='tight')

		G1pred_tr = SP.array((SP.sum(predRF_trv[labels==1]==1),SP.sum(predRF_trv[labels==2]==1),SP.sum(predRF_trv[labels==3]==1)))
		Spred_tr = SP.array((SP.sum(predRF_trv[labels==1]==2),SP.sum(predRF_trv[labels==2]==2),SP.sum(predRF_trv[labels==3]==2)))
		G2Mpred_tr = SP.array((SP.sum(predRF_trv[labels==1]==3),SP.sum(predRF_trv[labels==2]==3),SP.sum(predRF_trv[labels==3]==3)))
		class_labels = ['G1', 'S', 'G2M']
		ind_b = SP.arange(3)
		if do_h == True:
			PL.figure(figsize=(5,2))
			p1cv = plt.barh(ind_b, G1pred_tr,   height=0.85, color=cols[0])
			p2cv = plt.barh(ind_b, Spred_tr,height=0.85, color=cols[1], left=G1pred_tr)
			p3cv = plt.barh(ind_b, G2Mpred_tr,height=0.85, color=cols[2], left=G1pred_tr+Spred_tr)
			PL.xlabel('# cells')
			PL.yticks(ind_b+width/2., class_labels )
		else:
			PL.figure(figsize=(5,5))
			p1cv = plt.barh(ind_b, G1pred_tr,   width, color=cols[0])
			p2cv = plt.barh(ind_b, Spred_tr,width, color=cols[1], left=G1pred_tr)
			p3cv = plt.barh(ind_b, G2Mpred_tr,width, color=cols[2], left=G1pred_tr+Spred_tr)
			PL.ylabel('# cells')
			PL.title('True cell cycle phase')
			PL.xticks(ind_b+width/2., class_labels )
		#PL.yticks(SP.arange(0,20,5))
		ax = PL.gca()
		ax.spines["right"].set_visible(False)
		ax.spines["top"].set_visible(False)
		ax.get_xaxis().tick_bottom()
		ax.get_yaxis().tick_left()
		lgd = PL.legend( (p1cv[0], p2cv[0],p3cv[0]), ('Pred. G1', 'Pred. S','Pred. G2M') ,loc='center left', bbox_to_anchor=(1, 0.5))
		PL.savefig(out_dir+'/barchart_cv'+method+'.pdf',bbox_extra_artists=(lgd,),bbox_inches='tight')

	def plotScatter(self, file_name = 'scatter_ts',out_dir = './cache', plot_test = True, xaxis = 0, yaxis = 2, xlab = 'G1 score', ylab = 'G2M score', class_labels = ['G1 phase','S phase','G2M phase'], method = 'RF', decision_lines=True):
		plparams = {'backend': 'pdf',
		  'axes.labelsize': 14,
		  'text.fontsize': 14,
		  'legend.fontsize': 13,
		  'xtick.labelsize': 14,
		  'ytick.labelsize': 14,
		  'text.usetex': False}
		PL.rcParams.update(plparams)
		assert self.scores != None, 'cyclone: first train the model before attempting to plot'
		if not os.path.exists(out_dir):
			os.makedirs(out_dir)
		file_name = file_name+method+'.pdf'
		if plot_test==False:
			file_name = file_name+method+'_cv.pdf'
		if plot_test ==True:
			if method=='RF':
				labs = self.labels_tst
				scores = self.scores_tst
			elif method=='LR':
				labs = self.labels_tst
				scores = self.scoresLR_tst
			elif method=='LRall':
				labs = self.labels_tst
				scores = self.scoresLRall_tst
			elif method=='GNB':
				labs = self.labels_tst
				scores = self.scoresGNB_tst
			elif method=='SVM':
				labs = self.labels_tst
				scores = self.scoresSVM_tst
			elif method=='SVMrbf':
				labs = self.labels_tst
				scores = self.scoresSVMrbf_tst
		else:
			if method=='RF':
				labs = self.labels
				scores = self.scores
			elif method=='LR':
				labs = self.labels
				scores = self.scoresLR
			elif method=='LRall':
				labs = self.labels
				scores = self.scoresLRall
			elif method=='GNB':
				labs = self.labels
				scores = self.scoresGNB
			elif method=='SVM':
				labs = self.labels
				scores = self.scoresSVM
			elif method=='SVMrbf':
				labs = self.labels
				scores = self.scoresSVMrbf
		cols = ['r', 'b', 'g', 'y', 'Crimson', 'DeepPink','LightSalmon','Lime', 'Olive']
		cols_d = {}
		cols_d['G1'] = '#1b9e77'
		cols_d['S'] = '#d95f02'
		cols_d['G2M'] = '#7570b3'
		cols_d['mid'] = '#e7298a'
		cols_d['early'] = '#e6ab02'
		cols_d['late'] = '#66a61e'

		labs = labs.astype('int')
		lab_col = list()
		fig = PL.figure(figsize=(6,6))
		ax = fig.add_subplot(111)
		#ax=PL.axes([0.25,0.25,0.65,0.65])
		ax.set_position([0.1,0.1,0.7,0.7])
		hList =list()
		for iplot in range(len(labs)):
			#hList.append(PL.plot(scores[iplot,xaxis],scores[iplot,yaxis],'.',markersize=15,c=cols[labs[iplot]-1], alpha=0.75))
			if class_labels[labs[iplot]-1] in cols_d.keys():
				hList.append(PL.plot(scores[iplot,xaxis],scores[iplot,yaxis],'.',markersize=15,c=cols_d[class_labels[labs[iplot]-1]], alpha=0.75))
			else:
				hList.append(PL.plot(scores[iplot,xaxis],scores[iplot,yaxis],'.',markersize=15,c='#8c510a', alpha=0.75))
		PL.xlabel(xlab)
		PL.ylabel(ylab)
		if xlab == 'G1 score':
			PL.xlim(xmin = 0.0, xmax = .95)
			x_max = 0.75
		else:
			x_max = scores[:,xaxis].max() #+ 0.05
			PL.xlim(xmax = x_max+0.05)
		if ylab == 'G2M score':
			PL.ylim(ymin = 0.0, ymax = .95)
			y_max = 0.75
		else:
			y_max = scores[:,yaxis].max() #+ 0.05
			PL.ylim(ymax = y_max+0.05)

		if decision_lines ==True:
			x_min = 0.0
			y_min = 0.0
			h = 0.001
			xx, yy = SP.meshgrid(SP.arange(x_min, x_max, h),
							 SP.arange(y_min, y_max, h))
			zz = 1 - (xx + yy)
			Z = SP.argmax(SP.dstack((xx,yy,zz)), 2)
			PL.contour(xx, yy, Z, levels = [0,1])

		legH=list()
		u_classes = SP.unique(labs)
		for ileg in u_classes:
			legH.append(hList[SP.where(labs==ileg)[0][0]][0])
		lh=PL.legend(legH,class_labels,loc='upper center',bbox_to_anchor=(0.5, 1.15),ncol=3, numpoints=1,scatterpoints=1)
		lh.set_frame_on(False)
		ax.spines["right"].set_visible(False)
		ax.spines["top"].set_visible(False)
		ax.get_xaxis().tick_bottom()
		PL.savefig(out_dir+'/'+file_name,bbox_extra_artists=[lh])#,bbox_inches='tight')

		#print("Generating HDF5 result file ...")

		#fi = h5py.File(CFG['out_file'],'w')
		#fi['X'] = Y
		#fi['labels'] = labels
		#fi['predLR'] = predLR
		#fi['predLR_ts'] = predLR_ts
		#fi['predRF'] = predRF
		#fi['scores'] = scores
		#fi.close()
	def plotPerformance(self,perfType = 'ROC',plot_test=True, out_dir = './cache',class_labels = ['G1 phase','S phase','G2M phase'], method = 'RF'):
		plparams = {'backend': 'pdf',
		  'axes.labelsize': 14,
		  'text.fontsize': 14,
		  'legend.fontsize': 13,
		  'xtick.labelsize': 14,
		  'ytick.labelsize': 14,
		  'text.usetex': False}
		PL.rcParams.update(plparams)
		assert(perfType in ['ROC', 'PR'])
		if plot_test ==True:
			if method=='RF':
				labs = self.labels_tst
				scores = self.scores_tst
			elif method=='LR':
				labs = self.labels_tst
				scores = self.scoresLR_tst
			elif method=='LRall':
				labs = self.labels_tst
				scores = self.scoresLRall_tst
			elif method=='GNB':
				labs = self.labels_tst
				scores = self.scoresGNB_tst
			elif method=='SVM':
				labs = self.labels_tst
				scores = self.scoresSVM_tst
			elif method=='SVMrbf':
				labs = self.labels_tst
				scores = self.scoresSVMrbf_tst
		else:
			if method=='RF':
				labs = self.labels
				scores = self.scores
			elif method=='LR':
				labs = self.labels
				scores = self.scoresLR
			elif method=='LRall':
				labs = self.labels
				scores = self.scoresLRall
			elif method=='GNB':
				labs = self.labels
				scores = self.scoresGNB
			elif method=='SVM':
				labs = self.labels
				scores = self.scoresSVM
			elif method=='SVMrbf':
				labs = self.labels
				scores = self.scoresSVMrbf
		PL.figure()
		#col = ['r', 'b', 'g']
		col = ['#1b9e77', '#d95f02', '#7570b3']
		aucList = SP.zeros((scores.shape[1],))
		for ind in range(scores.shape[1]):
			labels_i = labs.copy()
			scores_i = scores[:,ind]
			labels_i[labs==ind+1] = 1
			labels_i[labs!=ind+1] = 0
			if perfType == 'ROC':
				fpr, tpr, thresholds = metrics.roc_curve(labels_i, scores_i)
				aucList[ind] = metrics.auc(fpr, tpr)
			elif perfType == 'PR':
				fpr, tpr, thresholds = metrics.precision_recall_curve(labels_i, scores_i)
			PL.plot(fpr, tpr, '-', c=col[ind])
		if perfType=='ROC':
			PL.title("ROC curve ccClassify")
			PL.xlabel('FPR')
			PL.ylabel('TPR')
			leg_str = list()
			for i in range(len(class_labels)):
				leg_str.append(class_labels[i]+', AUC = '+str(SP.round_(aucList[i],3)))
			PL.legend(leg_str, loc='lower-right')
			ax = plt.gca()
			ax.spines["right"].set_visible(False)
			ax.spines["top"].set_visible(False)
			ax.get_xaxis().tick_bottom()
			ax.get_yaxis().tick_left()
			PL.savefig(out_dir+'/ROC_test'+str(plot_test)+'_'+method+'.pdf',bbox_inches='tight')
		else:
			PL.title("PR curve ccClassify")
			PL.xlabel('Precision')
			PL.ylabel('Recall')
			leg_str = list()
			for i in range(len(class_labels)):
				leg_str.append(class_labels[i])#+', AUC = '+str(SP.round_(aucList[i],3)))
			PL.legend(leg_str, loc='lower-right')
			ax = plt.gca()
			ax.spines["right"].set_visible(False)
			ax.spines["top"].set_visible(False)
			ax.get_xaxis().tick_bottom()
			ax.get_yaxis().tick_left()
			PL.savefig(out_dir+'/ROC_test'+str(plot_test)+'_'+method+'.pdf',bbox_inches='tight')

	def plotF1(self,plot_test=True, out_dir = './cache',class_labels = ['G1','S','G2M']):
		plparams = {'backend': 'pdf',
		  'axes.labelsize': 14,
		  'text.fontsize': 14,
		  'legend.fontsize': 13,
		  'xtick.labelsize': 14,
		  'ytick.labelsize': 14,
		  'text.usetex': False}
		PL.rcParams.update(plparams)
		fig = PL.figure(figsize=(6,6))
		ax = fig.add_subplot(111)
		#ax=PL.axes([0.25,0.25,0.65,0.65])
		ax.set_position([0.1,0.1,0.7,0.7])
		hList =list()
		cols_d = {}
		cols_d['G1'] = '#1b9e77'
		cols_d['S'] = '#d95f02'
		cols_d['G2M'] = '#7570b3'
		cols_d['mid'] = '#e7298a'
		cols_d['early'] = '#e6ab02'
		cols_d['late'] = '#66a61e'
		m_i = 0
		all_meth = ['GNB','RF', 'LR', 'LRall','SVM']
		for method in all_meth:
			if plot_test ==True:
				if method=='RF':
					labs = self.labels_tst
					scores = self.scores_tst
				elif method=='LR':
					labs = self.labels_tst
					scores = self.scoresLR_tst
				elif method=='LRall':
					labs = self.labels_tst
					scores = self.scoresLRall_tst
				elif method=='GNB':
					labs = self.labels_tst
					scores = self.scoresGNB_tst
				elif method=='SVM':
					labs = self.labels_tst
					scores = self.scoresSVM_tst
				elif method=='SVMrbf':
					labs = self.labels_tst
					scores = self.scoresSVMrbf_tst
			else:
				if method=='RF':
					labs = self.labels
					scores = self.scores
				elif method=='LR':
					labs = self.labels
					scores = self.scoresLR
				elif method=='LRall':
					labs = self.labels
					scores = self.scoresLRall
				elif method=='GNB':
					labs = self.labels
					scores = self.scoresGNB
				elif method=='SVM':
					labs = self.labels
					scores = self.scoresSVM
				elif method=='SVMrbf':
					labs = self.labels
					scores = self.scoresSVMrbf
			pred = SP.argmax(scores,axis=1)+1

			f1_scores = metrics.f1_score(labs, pred, average=None)
			f1_score_av = metrics.f1_score(labs, pred, average="macro")

			for iplot in range(len(f1_scores)):
				#hList.append(PL.plot(scores[iplot,xaxis],scores[iplot,yaxis],'.',markersize=15,c=cols[labs[iplot]-1], alpha=0.75))
				if class_labels[iplot] in cols_d.keys():
					hList.append(PL.plot(m_i,f1_scores[iplot],'^',markersize=15,c=cols_d[class_labels[iplot]], alpha=0.65))
			PL.plot([m_i-0.25, m_i+0.25], [f1_score_av,f1_score_av], linewidth=2, color='r', hold=True)
			m_i+=1.0
		PL.xticks(range(len(all_meth)), all_meth, rotation=46)
		PL.ylabel('F1 score')
		PL.ylim(ymin = -0.05, ymax = 1.05)
		ax = plt.gca()
		ax.spines["right"].set_visible(False)
		ax.spines["top"].set_visible(False)
		ax.get_xaxis().tick_bottom()
		ax.get_yaxis().tick_left()
		PL.savefig(out_dir+'/F1_test'+str(plot_test)+'_'+method+'.pdf',bbox_inches='tight')

max_stars_repo_name: PMBio/cyclone

max_issues_repo_name: PMBio/cyclone

max_forks_repo_name: PMBio/cyclone

reason: from numpy,import scipy