text:
from csv import reader
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import pickle
import joblib
import argparse

def split_train_val_test(sample_list, ratio = [0.8, 0.1]):
    sample_num = len(sample_list)
    np.random.shuffle(sample_list)

    train_size = int(sample_num * 0.8)
    val_size = int(sample_num * 0.1)
    test_size = sample_num - train_size - val_size

    train_list = sample_list[0:train_size]
    val_list = sample_list[train_size+1:train_size+val_size]
    test_list = sample_list[train_size+val_size+1:]

    return train_list, val_list, test_list

def split_train_test(sample_list, ratio=0.8):
    sample_num = len(sample_list)
    np.random.shuffle(sample_list)

    train_size = int(sample_num * 0.8)
    test_size = sample_num - train_size
    train_list = sample_list[0:train_size]
    test_list = sample_list[train_size:]

    return train_list, test_list

def one_hot(a, num_classes):
    return np.squeeze(np.eye(num_classes)[a.reshape(-1)])

if __name__ == "__main__":
    # specifying data paths
    parser = argparse.ArgumentParser(description = 'Data Loading Pipeline',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--data_dir', default='data_stranded', type=str, help='Specify the data directory')
    parser.add_argument('--type', default='gene', type=str, help='Specify the hidden nodes of 1st layer')
    print("Loading Data")
    args = parser.parse_args()

    if args.type == 'exon':
        data_dir = '../' + args.data_dir
        #COPDGene_Freeze1_RNAseq_genes = os.path.join(data_dir, 'COPDGene_Freeze3_RNAseq_genes.csv')
        #COPDGene_Freeze1_RNAseq_exonicParts_logCPM_covAdjusted = os.path.join(data_dir, 'COPDGene_Freeze3_RNAseq_exonicParts_logCPM_covAdjustedWOsv.csv')
        COPDGene_Freeze1_RNAseq_exonicParts_logCPM_normalized = os.path.join(data_dir, 'COPDGene_Freeze3_RNAseq_exonicParts_logCPM_normalized.csv')
        COPDGene_Freeze1_RNAseq_samples = os.path.join(data_dir, 'COPDGene_Freeze3_RNAseq_samples.csv')

        # loading csv data to pandas frame
        # genes = pd.read_csv(COPDGene_Freeze1_RNAseq_genes)
        #exonicParts_logCPM_covAdjusted = pd.read_csv(COPDGene_Freeze1_RNAseq_exonicParts_logCPM_covAdjusted)
        print("Reading csv files")
        exonicParts_logCPM_normalized = pd.read_csv(COPDGene_Freeze1_RNAseq_exonicParts_logCPM_normalized)
        samples = pd.read_csv(COPDGene_Freeze1_RNAseq_samples)

        # dealing with data
        # so every row represents a patient for now
        # shape (520, 266228)
        #exonicParts_logCPM_covAdjusted_matrix = np.transpose(exonicParts_logCPM_covAdjusted.values[:, 1:])
        print("Loading to matrix")
        exonicParts_logCPM_normalized_matrix = np.transpose(exonicParts_logCPM_normalized.values[:, 1:])

        # get smoking status
        # shape (520, )
        smoking_status = samples['SmokCigNow'].values
        # TODO:!!!!!
        # smoking_status_matrix = one_hot(smoking_status, 2)

        # total number of examples
        sample_num_total = exonicParts_logCPM_normalized_matrix.shape[0]
        assert(smoking_status.shape[0] == sample_num_total)

        # get train, val, test indices
        pos_list = np.where(smoking_status==1)
        neg_list = np.where(smoking_status==0)

        train_list_pos, val_list_pos, test_list_pos = split_train_val_test(np.arange(sample_num_total)[pos_list])
        train_list_neg, val_list_neg, test_list_neg = split_train_val_test(np.arange(sample_num_total)[neg_list])
        #train_list_pos, test_list_pos = split_train_test(np.arange(sample_num_total)[pos_list])
        #train_list_neg, test_list_neg = split_train_test(np.arange(sample_num_total)[neg_list])

        train_list = np.concatenate((train_list_pos, train_list_neg), axis=0)
        val_list = np.concatenate((val_list_pos, val_list_neg), axis=0)
        # we keep test as a held-out dataset
        test_list = np.concatenate((test_list_pos, test_list_neg), axis=0)

        # saving them to proper place using pickle
        print("Saving to pickle")
        saving_dir = '../' + args.data_dir
        #covAdj_path = os.path.join(saving_dir, 'COPDGene_Freeze3_RNAseq_exonicParts_logCPM_covAdjusted')
        normalized_path = os.path.join(saving_dir, 'COPDGene_Freeze3_RNAseq_exonicParts_logCPM_normalized')
        smoking_path = os.path.join(saving_dir, 'COPDGene_Freeze3_RNAseq_samples', 'SmokCigNow')
        #if not os.path.exists(covAdj_path):
        #    os.mkdir(covAdj_path)
        if not os.path.exists(normalized_path):
            os.mkdir(normalized_path)
        if not os.path.exists(smoking_path):
            os.mkdir(smoking_path)

        # here we should dump the partition list as well
        pickle.dump(train_list, open(os.path.join(saving_dir, 'train_list.pickle'), 'wb'))
        pickle.dump(val_list, open(os.path.join(saving_dir, 'val_list.pickle'), 'wb'))
        pickle.dump(test_list, open(os.path.join(saving_dir, 'test_list.pickle'), 'wb'))

        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[train_list], open(os.path.join(covAdj_path, 'X_train.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[val_list], open(os.path.join(covAdj_path, 'X_val.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[test_list], open(os.path.join(covAdj_path, 'X_test.pickle'),'wb'))

        joblib.dump(exonicParts_logCPM_normalized_matrix[train_list], open(os.path.join(normalized_path, 'X_train.pickle'),'wb'), protocol=2)
        joblib.dump(exonicParts_logCPM_normalized_matrix[val_list], open(os.path.join(normalized_path, 'X_val.pickle'),'wb'), protocol=2)
        joblib.dump(exonicParts_logCPM_normalized_matrix[test_list], open(os.path.join(normalized_path, 'X_test.pickle'),'wb'), protocol=2)

        joblib.dump(smoking_status[train_list], open(os.path.join(smoking_path, 'Y_train.pickle'),'wb'))
        joblib.dump(smoking_status[val_list], open(os.path.join(smoking_path, 'Y_val.pickle'),'wb'))
        joblib.dump(smoking_status[test_list], open(os.path.join(smoking_path, 'Y_test.pickle'),'wb'))

        # here we directly dump X and Y all
        #print("Saving all the data for now")
        #pickle.dump(exonicParts_logCPM_normalized_matrix, open(os.path.join(normalized_path, 'X_all.pickle'),'wb'))
        #pickle.dump(smoking_status, open(os.path.join(smoking_path, 'Y_all.pickle'),'wb'))

    elif args.type == 'gene':
        data_dir = '../' + args.data_dir
        saving_dir = '../' + args.data_dir
        train_list = pickle.load(open(os.path.join(saving_dir, 'train_list.pickle'), 'rb'))
        val_list = pickle.load(open(os.path.join(saving_dir, 'val_list.pickle'), 'rb'))
        test_list = pickle.load(open(os.path.join(saving_dir, 'test_list.pickle'), 'rb'))
        #COPDGene_Freeze1_RNAseq_exonicParts_logCPM_covAdjusted = os.path.join(data_dir, 'COPDGene_Freeze3_RNAseq_genes_logCPM_covAdjusted.csv')
        COPDGene_Freeze1_RNAseq_exonicParts_logCPM_normalized = os.path.join(data_dir, 'COPDGene_Freeze3_RNAseq_genes_logCPM_normalized.csv')

        # loading csv data to pandas frame

        #exonicParts_logCPM_covAdjusted = pd.read_csv(COPDGene_Freeze1_RNAseq_exonicParts_logCPM_covAdjusted)
        exonicParts_logCPM_normalized = pd.read_csv(COPDGene_Freeze1_RNAseq_exonicParts_logCPM_normalized)

        # dealing with data
        # so every row represents a patient for now
        # shape (520, 266228)
        #exonicParts_logCPM_covAdjusted_matrix = np.transpose(exonicParts_logCPM_covAdjusted.values[:, 1:])
        exonicParts_logCPM_normalized_matrix = np.transpose(exonicParts_logCPM_normalized.values[:, 1:])

        # get smoking status
        # shape (520, )
        # smoking_status = samples['SmokCigNow'].values
        # TODO:!!!!!
        # smoking_status_matrix = one_hot(smoking_status, 2)

        # total number of examples
        sample_num_total = exonicParts_logCPM_normalized_matrix.shape[0]
        # assert(smoking_status.shape[0] == sample_num_total)

        # get train, val, test indices
        #pos_list = np.where(smoking_status==1)
        #neg_list = np.where(smoking_status==0)

        #train_list_pos, val_list_pos, test_list_pos = split_train_val_test(np.arange(sample_num_total)[pos_list])
        #train_list_neg, val_list_neg, test_list_neg = split_train_val_test(np.arange(sample_num_total)[neg_list])

        #train_list = np.concatenate((train_list_pos, train_list_neg), axis=0)
        #val_list = np.concatenate((val_list_pos, val_list_neg), axis=0)
        #test_list = np.concatenate((test_list_pos, test_list_neg), axis=0)

        # saving them to proper place using pickle
        saving_dir = '../' + args.data_dir
        #covAdj_path = os.path.join(saving_dir, 'COPDGene_Freeze1_RNAseq_exonicParts_logCPM_covAdjusted')
        normalized_path = os.path.join(saving_dir, 'COPDGene_Freeze3_RNAseq_genes_logCPM_normalized')
        #smoking_path = os.path.join(saving_dir, 'COPDGene_Freeze1_RNAseq_samples', 'SmokCigNow')

        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[train_list], open(os.path.join(covAdj_path, 'X_train.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[val_list], open(os.path.join(covAdj_path, 'X_val.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[test_list], open(os.path.join(covAdj_path, 'X_test.pickle'),'wb'))

        joblib.dump(exonicParts_logCPM_normalized_matrix[train_list], open(os.path.join(normalized_path, 'X_train.pickle'),'wb'), protocol=2)
        joblib.dump(exonicParts_logCPM_normalized_matrix[val_list], open(os.path.join(normalized_path, 'X_val.pickle'),'wb'), protocol=2)
        joblib.dump(exonicParts_logCPM_normalized_matrix[test_list], open(os.path.join(normalized_path, 'X_test.pickle'),'wb'), protocol=2)

        #pickle.dump(smoking_status[train_list], open(os.path.join(smoking_path, 'Y_train.pickle'),'wb'))
        #pickle.dump(smoking_status[val_list], open(os.path.join(smoking_path, 'Y_val.pickle'),'wb'))
        #pickle.dump(smoking_status[test_list], open(os.path.join(smoking_path, 'Y_test.pickle'),'wb'))

        # here we directly dump X and Y all
        #print("Saving all the data for now")
        #pickle.dump(exonicParts_logCPM_normalized_matrix, open(os.path.join(normalized_path, 'X_all.pickle'),'wb'))
        #pickle.dump(smoking_status, open(os.path.join(smoking_path, 'Y_all.pickle'),'wb'))

    elif args.type == 'trans':
        data_dir = '../' + args.data_dir
        saving_dir = '../' + args.data_dir
        train_list = pickle.load(open(os.path.join(saving_dir, 'train_list.pickle'), 'rb'))
        val_list = pickle.load(open(os.path.join(saving_dir, 'val_list.pickle'), 'rb'))
        test_list = pickle.load(open(os.path.join(saving_dir, 'test_list.pickle'), 'rb'))
        COPDGene_Freeze1_RNAseq_exonicParts_logCPM_normalized = os.path.join(data_dir, 'COPDGene_Freeze3_RNAseq_transcripts_logCPM_normalized.csv')

        # loading csv data to pandas frame

        #exonicParts_logCPM_covAdjusted = pd.read_csv(COPDGene_Freeze1_RNAseq_exonicParts_logCPM_covAdjusted)
        exonicParts_logCPM_normalized = pd.read_csv(COPDGene_Freeze1_RNAseq_exonicParts_logCPM_normalized)

        # dealing with data
        # so every row represents a patient for now
        # shape (520, 266228)
        # exonicParts_logCPM_covAdjusted_matrix = np.transpose(exonicParts_logCPM_covAdjusted.values[:, 1:])
        exonicParts_logCPM_normalized_matrix = np.transpose(exonicParts_logCPM_normalized.values[:, 1:])

        # get smoking status
        # shape (520, )
        # smoking_status = samples['SmokCigNow'].values
        # TODO:!!!!!
        # smoking_status_matrix = one_hot(smoking_status, 2)

        # total number of examples
        # sample_num_total = exonicParts_logCPM_covAdjusted_matrix.shape[0]
        # assert(smoking_status.shape[0] == sample_num_total)

        # get train, val, test indices
        #pos_list = np.where(smoking_status==1)
        #neg_list = np.where(smoking_status==0)

        #train_list_pos, val_list_pos, test_list_pos = split_train_val_test(np.arange(sample_num_total)[pos_list])
        #train_list_neg, val_list_neg, test_list_neg = split_train_val_test(np.arange(sample_num_total)[neg_list])

        #train_list = np.concatenate((train_list_pos, train_list_neg), axis=0)
        #val_list = np.concatenate((val_list_pos, val_list_neg), axis=0)
        #test_list = np.concatenate((test_list_pos, test_list_neg), axis=0)

        # saving them to proper place using pickle
        saving_dir = '../' + args.data_dir
        #covAdj_path = os.path.join(saving_dir, 'COPDGene_Freeze1_RNAseq_exonicParts_logCPM_covAdjusted')
        normalized_path = os.path.join(saving_dir, 'COPDGene_Freeze3_RNAseq_transcripts_logCPM_normalized')

        joblib.dump(exonicParts_logCPM_normalized_matrix[train_list], open(os.path.join(normalized_path, 'X_train.pickle'),'wb'), protocol=2)
        joblib.dump(exonicParts_logCPM_normalized_matrix[val_list], open(os.path.join(normalized_path, 'X_val.pickle'),'wb'), protocol=2)
        joblib.dump(exonicParts_logCPM_normalized_matrix[test_list], open(os.path.join(normalized_path, 'X_test.pickle'),'wb'), protocol=2)

        #smoking_path = os.path.join(saving_dir, 'COPDGene_Freeze1_RNAseq_samples', 'SmokCigNow')

        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[train_list], open(os.path.join(covAdj_path, 'X_train.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[val_list], open(os.path.join(covAdj_path, 'X_val.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_covAdjusted_matrix[test_list], open(os.path.join(covAdj_path, 'X_test.pickle'),'wb'))

        #pickle.dump(exonicParts_logCPM_normalized_matrix[train_list], open(os.path.join(normalized_path, 'X_train.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_normalized_matrix[val_list], open(os.path.join(normalized_path, 'X_val.pickle'),'wb'))
        #pickle.dump(exonicParts_logCPM_normalized_matrix[test_list], open(os.path.join(normalized_path, 'X_test.pickle'),'wb'))

        #pickle.dump(smoking_status[train_list], open(os.path.join(smoking_path, 'Y_train.pickle'),'wb'))
        #pickle.dump(smoking_status[val_list], open(os.path.join(smoking_path, 'Y_val.pickle'),'wb'))
        #pickle.dump(smoking_status[test_list], open(os.path.join(smoking_path, 'Y_test.pickle'),'wb'))

        # here we directly dump X and Y all
        #pickle.dump(smoking_status, open(os.path.join(smoking_path, 'Y_all.pickle'),'wb'))

max_stars_repo_name: KingSpencer/COPD-IsoMap

max_issues_repo_name: KingSpencer/COPD-IsoformMap

max_forks_repo_name: KingSpencer/COPD-IsoformMap


text:
import logging
import math
import os
from glob import glob
from typing import List, Tuple

import numpy as np
import rasterio
import rasterio.merge
import rasterio.windows
from rasterio.transform import Affine
from tqdm import tqdm

_logger = logging.getLogger(__name__)

def get_bounds_from_image_files(image_files: List[str]) -> Tuple[float]:
    """Get bounds from all images, and transform to pixels based on the affine transform"""
    xs = []
    ys = []
    for img_path in tqdm(image_files):
        with rasterio.open(img_path) as src:
            left, bottom, right, top = src.bounds
        xs.extend([left, right])
        ys.extend([bottom, top])
    dst_w, dst_s, dst_e, dst_n = min(xs), min(ys), max(xs), max(ys)
    return (dst_w, dst_s, dst_e, dst_n)

def crop_image(img: np.ndarray, margin_ratio: float) -> np.ndarray:
    """Center crop an image, with a margin of ``margin_ratio``"""
    h, w = img.shape[0], img.shape[1]
    h_margin = math.floor(h * margin_ratio)
    w_margin = math.floor(w * margin_ratio)
    return img[h_margin:-h_margin, w_margin:-w_margin]

def merge(images_dir: str, output_path: str, crop_margin_ratio: float = 0.125):
    """Merge all images in ``images_dir`` into a single image"""
    image_paths = glob(os.path.join(images_dir, "*.tif"))
    if not image_paths:
        raise RuntimeError("images_dir does not contain any .tif file")

    # Get the profile and affine of some image as template for output image
    with rasterio.open(image_paths[0]) as src:
        profile = src.profile.copy()
        src_res = src.res
        src_count = src.count
        # nodataval = src.nodatavals[0]
        # dt = src.dtypes[0]
        # transform = src.transform

    # Get bounds from all images, and transform to (width, height) in pixels
    dst_w, dst_s, dst_e, dst_n = get_bounds_from_image_files(image_paths)

    _logger.debug("Output bounds: %r", (dst_w, dst_s, dst_e, dst_n))
    output_transform = Affine.translation(dst_w, dst_n)
    _logger.debug("Output transform, before scaling: %r", output_transform)

    output_transform *= Affine.scale(src_res[0], -src_res[1])
    _logger.debug("Output transform, after scaling: %r", output_transform)

    # Compute output array shape. We guarantee it will cover the output
    # bounds completely
    output_width = int(math.ceil((dst_e - dst_w) / src_res[0]))
    output_height = int(math.ceil((dst_n - dst_s) / src_res[1]))

    # Adjust bounds to fit
    dst_e, dst_s = output_transform * (output_width, output_height)
    _logger.debug("Output width: %d, height: %d", output_width, output_height)
    _logger.debug("Adjusted bounds: %r", (dst_w, dst_s, dst_e, dst_n))

    # Set width and height, and other attributes
    profile.update(
        width=output_width, height=output_height, transform=output_transform, tiled=True
    )

    # Call rasterio.merge using windowed reading-writing
    # and a custom callable that center-crops image.
    with rasterio.open(output_path, "w", **profile) as dst:
        for image_path in image_paths:
            with rasterio.open(image_path) as src:
                img = np.dstack(src.read())
                b = src.bounds
                window = rasterio.windows.from_bounds(
                    b[0], b[1], b[2], b[3], output_transform
                )
                # window = window.round_shape()

            cropped_img = crop_image(img, margin_ratio=crop_margin_ratio)
            # cropped_img = img.copy()

            for b in range(src_count):
                dst.write(cropped_img[:, :, b], b + 1, window=window)

if __name__ == "__main__":
    import sys

    merge(sys.argv[1], sys.argv[2])

ext: py

max_stars_repo_name: dymaxionlabs/unetseg

max_issues_repo_name: dymaxionlabs/unetseg

max_forks_repo_name: dymaxionlabs/unetseg

reason: import numpy


text:
from torch.utils.data import DataLoader
import numpy as np
import torch
import os

import sys
sys.path.append(os.path.dirname("../"))

from lib.dataset.shapenet import shapenet
from lib.utils.pointcloud_io import write
from models.model_ptnet import PointNet_SSNx
from lib.ssn.ssn import soft_slic_pknn

@torch.no_grad()
def inference(pointcloud, pos_scale=10, weight=None):
    """generate 3d spix

    Args:
        pointcloud (Tensor): Tensor of input pointcloud
        pos_scale (int, optional): coordinate multpilter. Defaults to 10.
        weight ([type], optional): model itself. Defaults to None.

    Returns:
        [type]: [description]
    """
    if weight is not None:
        model = weight
    else:
        raise Exception('model not loaded')
    inputs = pos_scale * pointcloud
    inputs = inputs.to("cuda")

    Q, H, center, feature = model(inputs)

    Q = Q.to("cpu").detach().numpy()
    labels = H.to("cpu").detach().numpy()
    feature = feature.to("cpu").detach().numpy()
    center = center.to("cpu").detach().numpy()

    return Q, labels, center, feature

if __name__ == "__main__":
    import time
    import argparse
    import matplotlib.pyplot as plt
    parser = argparse.ArgumentParser()
    parser.add_argument("--weight",
                        "-w",
                        default='log/model-shapenet.pth',
                        type=str,
                        help="/path/to/pretrained_weight")
    parser.add_argument("--fdim",
                        "-d",
                        default=20,
                        type=int,
                        help="embedding dimension")
    parser.add_argument("--niter",
                        "-n",
                        default=10,
                        type=int,
                        help="number of iterations for differentiable SLIC")
    parser.add_argument("--nspix",
                        default=50,
                        type=int,
                        help="number of superpixels")
    parser.add_argument("--pos_scale", "-p", default=10, type=float)
    parser.add_argument("--folder",
                        "-f",
                        default='log',
                        help="a folder to store result")
    args = parser.parse_args()

    if not os.path.exists(args.folder):
        os.mkdir(args.folder)

    data = shapenet("../../shapenet_part_seg_hdf5_data", split='val')
    loader = DataLoader(data, batch_size=1, shuffle=False)
    model = PointNet_SSNx(args.fdim,
                         args.nspix,
                         args.niter,
                         backend=soft_slic_pknn).to("cuda")
    model.load_state_dict(torch.load(args.weight))
    model.eval()
    print(model)

    s = time.time()

    for i, (pointcloud, label, labell) in enumerate(loader):
        print(i)
        _, labels, _, _ = inference(pointcloud, 10, model)

        pointcloud = pointcloud.squeeze(0).transpose(1, 0).numpy()
        label = labell.transpose(1, 0).numpy()
        #spix = spix.squeeze(0).transpose(1,  0).numpy()
        ptcloud = np.concatenate(
            (pointcloud, label, label, labels.transpose(1, 0)), axis=-1)
        write.tobcd(ptcloud, 'xyzrgb',
                    os.path.join(args.folder, '{}.pcd'.format(i)))


text:
#!/usr/bin/env python2
from __future__ import division, print_function, absolute_import
from numpy.distutils.core import setup

def configuration(parent_package='', top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('mvtdstpack', parent_package, top_path)
    config.add_extension('mvtdstpack',
                         sources=['mvtdstpack_custom.pyf',
                                  'mvtdstpack.f'])
    return config

if __name__ == "__main__":
    setup(**configuration(top_path='').todict())

max_stars_repo_name: wgurecky/StarVine

max_issues_repo_name: wgurecky/StarVine

max_forks_repo_name: wgurecky/StarVine

reason: from numpy


text:
# Copyright (C) 2003-2005 Peter J. Verveer
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#
# 3. The name of the author may not be used to endorse or promote
#    products derived from this software without specific prior
#    written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import math
import numpy
from . import _ni_support
from kapteyn import _nd_image

def _extend_mode_to_code(mode):
    mode = _ni_support._extend_mode_to_code(mode)
    return mode

def spline_filter1d(input, order = 3, axis = -1, output = numpy.float64,
                    output_type = None):
    """Calculates a one-dimensional spline filter along the given axis.

    The lines of the array along the given axis are filtered by a
    spline filter. The order of the spline must be >= 2 and <= 5.
    """
    if order < 0 or order > 5:
        raise RuntimeError('spline order not supported')
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input,
                                                    output_type)
    if order in [0, 1]:
        output[...] = numpy.array(input)
    else:
        axis = _ni_support._check_axis(axis, input.ndim)
        _nd_image.spline_filter1d(input, order, axis, output)
    return return_value

def spline_filter(input, order = 3, output = numpy.float64,
                  output_type = None):
    """Multi-dimensional spline filter.

    Note: The multi-dimensional filter is implemented as a sequence of
    one-dimensional spline filters. The intermediate arrays are stored
    in the same data type as the output. Therefore, for output types
    with a limited precision, the results may be imprecise because
    intermediate results may be stored with insufficient precision.
    """
    if order < 2 or order > 5:
        raise RuntimeError('spline order not supported')
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input,
                                                    output_type)
    if order not in [0, 1] and input.ndim > 0:
        for axis in range(input.ndim):
            spline_filter1d(input, order, axis, output = output)
            input = output
    else:
        output[...] = input[...]
    return return_value

def geometric_transform(input, mapping, output_shape = None,
                        output_type = None, output = None, order = 3,
                        mode = 'constant', cval = 0.0, prefilter = True,
                        extra_arguments = (), extra_keywords = {}):
    """Apply an arbritrary geometric transform.

    The given mapping function is used to find, for each point in the
    output, the corresponding coordinates in the input. The value of the
    input at those coordinates is determined by spline interpolation of
    the requested order.

    mapping must be a callable object that accepts a tuple of length
    equal to the output array rank and returns the corresponding input
    coordinates as a tuple of length equal to the input array
    rank. Points outside the boundaries of the input are filled
    according to the given mode ('constant', 'nearest', 'reflect' or
    'wrap'). The output shape can optionally be given. If not given,
    it is equal to the input shape. The parameter prefilter determines
    if the input is pre-filtered before interpolation (necessary for
    spline interpolation of order > 1).  If False it is assumed that
    the input is already filtered. The extra_arguments and
    extra_keywords arguments can be used to provide extra arguments
    and keywords that are passed to the mapping function at each call.

    Example
    -------
    >>> a = arange(12.).reshape((4,3))
    >>> def shift_func(output_coordinates):
    ...     return (output_coordinates[0]-0.5, output_coordinates[1]-0.5)
    ...
    >>> print geometric_transform(a,shift_func)
    array([[ 0.    ,  0.    ,  0.    ],
           [ 0.    ,  1.3625,  2.7375],
           [ 0.    ,  4.8125,  6.1875],
           [ 0.    ,  8.2625,  9.6375]])
    """
    if order < 0 or order > 5:
        raise RuntimeError('spline order not supported')
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    if output_shape is None:
        output_shape = input.shape
    if input.ndim < 1 or len(output_shape) < 1:
        raise RuntimeError('input and output rank must be > 0')
    mode = _extend_mode_to_code(mode)
    if prefilter and order > 1:
        filtered = spline_filter(input, order, output = numpy.float64)
    else:
        filtered = input
    output, return_value = _ni_support._get_output(output, input,
                                        output_type, shape = output_shape)
    _nd_image.geometric_transform(filtered, mapping, None, None, None,
               output, order, mode, cval, extra_arguments, extra_keywords)
    return return_value

def map_coordinates(input, coordinates, output_type = None, output = None,
                order = 3, mode = 'constant', cval = 0.0, prefilter = True):
    """
    Map the input array to new coordinates by interpolation.

    The array of coordinates is used to find, for each point in the output,
    the corresponding coordinates in the input. The value of the input at
    those coordinates is determined by spline interpolation of the
    requested order.

    The shape of the output is derived from that of the coordinate
    array by dropping the first axis. The values of the array along
    the first axis are the coordinates in the input array at which the
    output value is found.

    Parameters
    ----------
    input : ndarray
            The input array
    coordinates : array_like
                  The coordinates at which `input` is evaluated.
    output_type : deprecated
                  Use `output` instead.
    output : dtype, optional
             If the output has to have a certain type, specify the dtype.
             The default behavior is for the output to have the same type
             as `input`.
    order : int, optional
            The order of the spline interpolation, default is 3.
            The order has to be in the range 0-5.
    mode : str, optional
           Points outside the boundaries of the input are filled according
           to the given mode ('constant', 'nearest', 'reflect' or 'wrap').
           Default is 'constant'.
    cval : scalar, optional
           Value used for points outside the boundaries of the input if
           `mode='constant`. Default is 0.0
    prefilter : bool, optional
                The parameter prefilter determines if the input is
                pre-filtered with `spline_filter`_ before interpolation
                (necessary for spline interpolation of order > 1).
                If False, it is assumed that the input is already filtered.

    Returns
    -------
    return_value : ndarray
                   The result of transforming the input. The shape of the
                   output is derived from that of `coordinates` by dropping
                   the first axis.

    See Also
    --------
    spline_filter, geometric_transform, scipy.interpolate

    Examples
    --------
    >>> import scipy.ndimage
    >>> a = np.arange(12.).reshape((4,3))
    >>> print a
    array([[  0.,   1.,   2.],
           [  3.,   4.,   5.],
           [  6.,   7.,   8.],
           [  9.,  10.,  11.]])
    >>> sp.ndimage.map_coordinates(a, [[0.5, 2], [0.5, 1]], order=1)
    [ 2.  7.]

    Above, the interpolated value of a[0.5, 0.5] gives output[0], while
    a[2, 1] is output[1].

    >>> inds = np.array([[0.5, 2], [0.5, 4]])
    >>> sp.ndimage.map_coordinates(a, inds, order=1, cval=-33.3)
    array([  2. , -33.3])
    >>> sp.ndimage.map_coordinates(a, inds, order=1, mode='nearest')
    array([ 2.,  8.])
    >>> sp.ndimage.map_coordinates(a, inds, order=1, cval=0, output=bool)
    array([ True, False], dtype=bool

    """
    if order < 0 or order > 5:
        raise RuntimeError('spline order not supported')
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    coordinates = numpy.asarray(coordinates)
    if numpy.iscomplexobj(coordinates):
        raise TypeError('Complex type not supported')
    output_shape = coordinates.shape[1:]
    if input.ndim < 1 or len(output_shape) < 1:
        raise RuntimeError('input and output rank must be > 0')
    if coordinates.shape[0] != input.ndim:
        raise RuntimeError('invalid shape for coordinate array')
    mode = _extend_mode_to_code(mode)
    if prefilter and order > 1:
        filtered = spline_filter(input, order, output = numpy.float64)
    else:
        filtered = input
    output, return_value = _ni_support._get_output(output, input,
                                        output_type, shape = output_shape)
    _nd_image.geometric_transform(filtered, None, coordinates, None, None,
               output, order, mode, cval, None, None)
    return return_value

def affine_transform(input, matrix, offset = 0.0, output_shape = None,
                     output_type = None, output = None, order = 3,
                     mode = 'constant', cval = 0.0, prefilter = True):
    """Apply an affine transformation.

    The given matrix and offset are used to find for each point in the
    output the corresponding coordinates in the input by an affine
    transformation. The value of the input at those coordinates is
    determined by spline interpolation of the requested order. Points
    outside the boundaries of the input are filled according to the given
    mode. The output shape can optionally be given. If not given it is
    equal to the input shape. The parameter prefilter determines if the
    input is pre-filtered before interpolation, if False it is assumed
    that the input is already filtered.

    The matrix must be two-dimensional or can also be given as a
    one-dimensional sequence or array. In the latter case, it is
    assumed that the matrix is diagonal. A more efficient algorithms
    is then applied that exploits the separability of the problem.
    """
    if order < 0 or order > 5:
        raise RuntimeError('spline order not supported')
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    if output_shape is None:
        output_shape = input.shape
    if input.ndim < 1 or len(output_shape) < 1:
        raise RuntimeError('input and output rank must be > 0')
    mode = _extend_mode_to_code(mode)
    if prefilter and order > 1:
        filtered = spline_filter(input, order, output = numpy.float64)
    else:
        filtered = input
    output, return_value = _ni_support._get_output(output, input,
                                        output_type, shape = output_shape)
    matrix = numpy.asarray(matrix, dtype = numpy.float64)
    if matrix.ndim not in [1, 2] or matrix.shape[0] < 1:
        raise RuntimeError('no proper affine matrix provided')
    if matrix.shape[0] != input.ndim:
        raise RuntimeError('affine matrix has wrong number of rows')
    if matrix.ndim == 2 and matrix.shape[1] != output.ndim:
        raise RuntimeError('affine matrix has wrong number of columns')
    if not matrix.flags.contiguous:
        matrix = matrix.copy()
    offset = _ni_support._normalize_sequence(offset, input.ndim)
    offset = numpy.asarray(offset, dtype = numpy.float64)
    if offset.ndim != 1 or offset.shape[0] < 1:
        raise RuntimeError('no proper offset provided')
    if not offset.flags.contiguous:
        offset = offset.copy()
    if matrix.ndim == 1:
        _nd_image.zoom_shift(filtered, matrix, offset, output, order,
                             mode, cval)
    else:
        _nd_image.geometric_transform(filtered, None, None, matrix, offset,
                            output, order, mode, cval, None, None)
    return return_value

def shift(input, shift, output_type = None, output = None, order = 3,
          mode = 'constant', cval = 0.0, prefilter = True):
    """Shift an array.

    The array is shifted using spline interpolation of the requested
    order. Points outside the boundaries of the input are filled according
    to the given mode. The parameter prefilter determines if the input is
    pre-filtered before interpolation, if False it is assumed that the
    input is already filtered.
    """
    if order < 0 or order > 5:
        raise RuntimeError('spline order not supported')
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    if input.ndim < 1:
        raise RuntimeError('input and output rank must be > 0')
    mode = _extend_mode_to_code(mode)
    if prefilter and order > 1:
        filtered = spline_filter(input, order, output = numpy.float64)
    else:
        filtered = input
    output, return_value = _ni_support._get_output(output, input,
                                                    output_type)
    shift = _ni_support._normalize_sequence(shift, input.ndim)
    shift = [-ii for ii in shift]
    shift = numpy.asarray(shift, dtype = numpy.float64)
    if not shift.flags.contiguous:
        shift = shift.copy()
    _nd_image.zoom_shift(filtered, None, shift, output, order, mode, cval)
    return return_value

def zoom(input, zoom, output_type = None, output = None, order = 3,
         mode = 'constant', cval = 0.0, prefilter = True):
    """Zoom an array.

    The array is zoomed using spline interpolation of the requested order.
    Points outside the boundaries of the input are filled according to the
    given mode. The parameter prefilter determines if the input is pre-
    filtered before interpolation, if False it is assumed that the input
    is already filtered.
    """
    if order < 0 or order > 5:
        raise RuntimeError('spline order not supported')
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    if input.ndim < 1:
        raise RuntimeError('input and output rank must be > 0')
    mode = _extend_mode_to_code(mode)
    if prefilter and order > 1:
        filtered = spline_filter(input, order, output = numpy.float64)
    else:
        filtered = input
    zoom = _ni_support._normalize_sequence(zoom, input.ndim)
    output_shape = tuple([int(ii * jj) for ii, jj in zip(input.shape, zoom)])
    zoom = (numpy.array(input.shape)-1)/(numpy.array(output_shape,float)-1)
    output, return_value = _ni_support._get_output(output, input,
                                        output_type, shape = output_shape)
    zoom = numpy.asarray(zoom, dtype = numpy.float64)
    zoom = numpy.ascontiguousarray(zoom)
    _nd_image.zoom_shift(filtered, zoom, None, output, order, mode, cval)
    return return_value

def _minmax(coor, minc, maxc):
    if coor[0] < minc[0]:
        minc[0] = coor[0]
    if coor[0] > maxc[0]:
        maxc[0] = coor[0]
    if coor[1] < minc[1]:
        minc[1] = coor[1]
    if coor[1] > maxc[1]:
        maxc[1] = coor[1]
    return minc, maxc

def rotate(input, angle, axes = (1, 0), reshape = True,
           output_type = None, output = None, order = 3,
           mode = 'constant', cval = 0.0, prefilter = True):
    """Rotate an array.

    The array is rotated in the plane defined by the two axes given by the
    axes parameter using spline interpolation of the requested order. The
    angle is given in degrees. Points outside the boundaries of the input
    are filled according to the given mode. If reshape is true, the output
    shape is adapted so that the input array is contained completely in
    the output. The parameter prefilter determines if the input is pre-
    filtered before interpolation, if False it is assumed that the input
    is already filtered.
    """
    input = numpy.asarray(input)
    axes = list(axes)
    rank = input.ndim
    if axes[0] < 0:
        axes[0] += rank
    if axes[1] < 0:
        axes[1] += rank
    if axes[0] < 0 or axes[1] < 0 or axes[0] > rank or axes[1] > rank:
        raise RuntimeError('invalid rotation plane specified')
    if axes[0] > axes[1]:
        axes = axes[1], axes[0]
    angle = numpy.pi / 180 * angle
    m11 = math.cos(angle)
    m12 = math.sin(angle)
    m21 = -math.sin(angle)
    m22 = math.cos(angle)
    matrix = numpy.array([[m11, m12],
                             [m21, m22]], dtype = numpy.float64)
    iy = input.shape[axes[0]]
    ix = input.shape[axes[1]]
    if reshape:
        mtrx = numpy.array([[ m11, -m21],
                               [-m12,  m22]], dtype = numpy.float64)
        minc = [0, 0]
        maxc = [0, 0]
        coor = numpy.dot(mtrx, [0, ix])
        minc, maxc = _minmax(coor, minc, maxc)
        coor = numpy.dot(mtrx, [iy, 0])
        minc, maxc = _minmax(coor, minc, maxc)
        coor = numpy.dot(mtrx, [iy, ix])
        minc, maxc = _minmax(coor, minc, maxc)
        oy = int(maxc[0] - minc[0] + 0.5)
        ox = int(maxc[1] - minc[1] + 0.5)
    else:
        oy = input.shape[axes[0]]
        ox = input.shape[axes[1]]
    offset = numpy.zeros((2,), dtype = numpy.float64)
    offset[0] = float(oy) / 2.0 - 0.5
    offset[1] = float(ox) / 2.0 - 0.5
    offset = numpy.dot(matrix, offset)
    tmp = numpy.zeros((2,), dtype = numpy.float64)
    tmp[0] = float(iy) / 2.0 - 0.5
    tmp[1] = float(ix) / 2.0 - 0.5
    offset = tmp - offset
    output_shape = list(input.shape)
    output_shape[axes[0]] = oy
    output_shape[axes[1]] = ox
    output_shape = tuple(output_shape)
    output, return_value = _ni_support._get_output(output, input,
                                        output_type, shape = output_shape)
    if input.ndim <= 2:
        affine_transform(input, matrix, offset, output_shape, None, output,
                         order, mode, cval, prefilter)
    else:
        coordinates = []
        size = numpy.product(input.shape,axis=0)
        size /= input.shape[axes[0]]
        size /= input.shape[axes[1]]
        for ii in range(input.ndim):
            if ii not in axes:
                coordinates.append(0)
            else:
                coordinates.append(slice(None, None, None))
        iter_axes = list(range(input.ndim))
        iter_axes.reverse()
        iter_axes.remove(axes[0])
        iter_axes.remove(axes[1])
        os = (output_shape[axes[0]], output_shape[axes[1]])
        for ii in range(size):
            ia = input[tuple(coordinates)]
            oa = output[tuple(coordinates)]
            affine_transform(ia, matrix, offset, os, None, oa, order, mode,
                             cval, prefilter)
            for jj in iter_axes:
                if coordinates[jj] < input.shape[jj] - 1:
                    coordinates[jj] += 1
                    break
                else:
                    coordinates[jj] = 0
    return return_value

max_stars_repo_name: kapteyn-astro/kapteyn

max_issues_repo_name: kapteyn-astro/kapteyn

max_forks_repo_name: kapteyn-astro/kapteyn

reason: import numpy,import scipy


text:
import argparse
import logging
import pandas as pd
import numpy as np
from collections import Counter
import os
from datetime import datetime

from tqdm.auto import tqdm

import catboost as cb

logger = logging.getLogger(__name__)

def parse_args():

    parser = argparse.ArgumentParser(
        description="Train models."
    )
    parser.add_argument(
        "--input_file", type=str, default=None, help="File containing the input data."
    )
    parser.add_argument(
        "--emb_folder", type=str, default=None, help="Folder containing the training embedding data."
    )
    parser.add_argument(
        "--models_folder", type=str, default=None, help="Folder containing the trained models."
    )
    parser.add_argument("--output_dir", type=str, default=None, help="Where to store the final embeddings.")
    parser.add_argument("--seed", type=int, default=0, help="A seed for reproducible training.")
    parser.add_argument(
        "--do_train",
        action="store_true",
        help="Train or no",
    )
    parser.add_argument(
        "--do_predict",
        action="store_true",
        help="Eval or no",
    )
    args = parser.parse_args()

    if args.output_dir is not None:
        os.makedirs(args.output_dir, exist_ok=True)

    return args

def main(args):

    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO,
    )

    input_df = pd.read_csv(args.input_file)
    fl_type = 'test'

    #Load training dataset
    logger.info("Training")
    lang_suffix = ['en', 'pt', 'es']

    ds_dict = {}
    preds_dict = {}

    for lang in lang_suffix:

        logger.info(f"Predicting on: {lang} data.")

        input_sub = input_df[input_df['Language'] == lang].copy()
        input_sub = input_sub.reset_index(drop=True)

        ds_dict[lang] = input_sub #save in dict, for future reference

        #Load embedding
        test_embeddings = np.load(os.path.join(args.emb_folder, f"{fl_type}_abstract_embedding_{lang}.npy"))

        pred_folds = []
        model_inputs = np.concatenate([np.expand_dims(np.array(input_sub['Year']), axis=1), test_embeddings], axis=1)

        for fold in range(5):
            logger.info(f"Lang: {lang} - Fold: {fold}")
            model = cb.CatBoostRegressor()
            path_str = os.path.join(args.models_folder, f"cat_{lang}_{fold}.cbm")
            model.load_model(path_str)

            prediction = model.predict(model_inputs, task_type='CPU')

            pred_folds.append(prediction)

        logger.info(f"Lang {lang}: predict the mean.")
        preds_dict[lang] = np.mean(np.array(pred_folds), axis=0)

    logger.info("Merge predictions into single file.")

    merged_preds = np.zeros(len(input_df))
    for lang in lang_suffix:

        for n, pred in enumerate(preds_dict[lang]):
            ix = ds_dict[lang].iloc[n]['id'] - 1
            merged_preds[ix] = pred

    final_preds = pd.DataFrame({'id': range(1, len(input_df)+1), 'total_rel_score': merged_preds})

    date = datetime.now().strftime("%Y_%m_%d-%I:%M:%S_%p")
    final_preds.to_csv(os.path.join(args.output_dir, f"SampleSubmission_{date}.csv"), index=False)

    logger.info("Prediction Done.")

if __name__ == "__main__":

    args = parse_args()

    main(args)


text:
from flask import Flask, flash, redirect, render_template, Response, request, session, abort
from imutils import paths
import face_recognition
import pickle
import cv2
import os
import shutil
import pandas as pd
import csv
import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm
from scipy import ndimage
from skimage.measure import regionprops
import pandas as pd
from time import time
import keras
from skimage import io
from skimage.filters import threshold_otsu
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

a = 0
usern = "Unknown"

def encode():

    imagePaths = list(paths.list_images('Images'))
    knownEncodings = []
    knownNames = []

    for (i, imagePath) in enumerate(imagePaths):

        name = imagePath.split(os.path.sep)[-2]

        image = cv2.imread(imagePath)
        rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        boxes = face_recognition.face_locations(rgb, model='hog')

        encodings = face_recognition.face_encodings(rgb, boxes)

        for encoding in encodings:
            knownEncodings.append(encoding)
            knownNames.append(name)

    data = {"encodings": knownEncodings, "names": knownNames}

    f = open("face_enc", "wb")
    f.write(pickle.dumps(data))
    f.close()

def recog():

    cascPathface = os.path.dirname(
        cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"

    faceCascade = cv2.CascadeClassifier(cascPathface)

    data = pickle.loads(open('face_enc', "rb").read())
    a = 0
    print("Streaming started")
    video_capture = cv2.VideoCapture(0)

    while True:

        ret, frame = video_capture.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = faceCascade.detectMultiScale(gray,
                                             scaleFactor=1.1,
                                             minNeighbors=5,
                                             minSize=(60, 60),
                                             flags=cv2.CASCADE_SCALE_IMAGE)

        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        encodings = face_recognition.face_encodings(rgb)
        names = []

        for encoding in encodings:

            matches = face_recognition.compare_faces(data["encodings"],
                                                     encoding)

            name = "Unknown"

            if True in matches:

                matchedIdxs = [i for (i, b) in enumerate(matches) if b]
                counts = {}

                for i in matchedIdxs:

                    name = data["names"][i]

                    counts[name] = counts.get(name, 0) + 1

                name = max(counts, key=counts.get)

            names.append(name)

            for ((x, y, w, h), name) in zip(faces, names):

                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                cv2.putText(frame, name, (x, y), cv2.FONT_HERSHEY_SIMPLEX,
                            0.75, (0, 255, 0), 2)
            a += 1
            global usern
            usern = name

        ret, buffer = cv2.imencode('.jpg', frame)
        frame = buffer.tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    video_capture.release()

genuine_image_paths = "Dataset/real/"
forged_image_paths = "Dataset/forged/"

def cap():
    while True:
        ret, frame = camera.read()
        ret, buffer = cv2.imencode('.jpg', frame)
        frame = buffer.tobytes()
        yield (b'--frame\r\n'
            b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

def regImage(un):
    if not (os.path.exists('Images/' + un)):
        os.mkdir('Images/' + un)

        cam=cv2.VideoCapture(0)
        for i in range(15):
            return_value, image = cam.read()
            if i > 4:
                cv2.imwrite('Images/' + un + '/' + un + str(i - 4) + '.png', image)

def rgbgrey(img):

    greyimg = np.zeros((img.shape[0], img.shape[1]))
    for row in range(len(img)):
        for col in range(len(img[row])):
            greyimg[row][col] = np.average(img[row][col])
    return greyimg

def greybin(img):

    blur_radius = 0.8

    img = ndimage.gaussian_filter(img, blur_radius)

    thres = threshold_otsu(img)
    binimg = img > thres
    binimg = np.logical_not(binimg)
    return binimg

def preproc(path, img=None, display=True):
    if img is None:
        img = mpimg.imread(path)
    if display:
        plt.imshow(img)
        plt.show()
    grey = rgbgrey(img)
    if display:
        plt.imshow(grey, cmap=matplotlib.cm.Greys_r)
        plt.show()
    binimg = greybin(grey)
    if display:
        plt.imshow(binimg, cmap=matplotlib.cm.Greys_r)
        plt.show()
    r, c = np.where(binimg == 1)

    signimg = binimg[r.min(): r.max(), c.min(): c.max()]
    if display:
        plt.imshow(signimg, cmap=matplotlib.cm.Greys_r)
        plt.show()
    return signimg

def Ratio(img):
    a = 0
    for row in range(len(img)):
        for col in range(len(img[0])):
            if img[row][col] == True:
                a = a + 1
    total = img.shape[0] * img.shape[1]
    return a / total

def Centroid(img):
    numOfWhites = 0
    a = np.array([0, 0])
    for row in range(len(img)):
        for col in range(len(img[0])):
            if img[row][col] == True:
                b = np.array([row, col])
                a = np.add(a, b)
                numOfWhites += 1
    rowcols = np.array([img.shape[0], img.shape[1]])
    centroid = a / numOfWhites
    centroid = centroid / rowcols
    return centroid[0], centroid[1]

def EccentricitySolidity(img):
    r = regionprops(img.astype("int8"))
    return r[0].eccentricity, r[0].solidity

def SkewKurtosis(img):
    h, w = img.shape
    x = range(w)
    y = range(h)

    xp = np.sum(img, axis=0)
    yp = np.sum(img, axis=1)

    cx = np.sum(x * xp) / np.sum(xp)
    cy = np.sum(y * yp) / np.sum(yp)

    x2 = (x - cx) ** 2
    y2 = (y - cy) ** 2
    sx = np.sqrt(np.sum(x2 * xp) / np.sum(img))
    sy = np.sqrt(np.sum(y2 * yp) / np.sum(img))

    x3 = (x - cx) ** 3
    y3 = (y - cy) ** 3
    skewx = np.sum(xp * x3) / (np.sum(img) * sx ** 3)
    skewy = np.sum(yp * y3) / (np.sum(img) * sy ** 3)

    x4 = (x - cx) ** 4
    y4 = (y - cy) ** 4

    kurtx = np.sum(xp * x4) / (np.sum(img) * sx ** 4) - 3
    kurty = np.sum(yp * y4) / (np.sum(img) * sy ** 4) - 3

    return (skewx, skewy), (kurtx, kurty)

def getFeatures(path, img=None, display=False):
    if img is None:
        img = mpimg.imread(path)
    img = preproc(path, display=display)
    ratio = Ratio(img)
    centroid = Centroid(img)
    eccentricity, solidity = EccentricitySolidity(img)
    skewness, kurtosis = SkewKurtosis(img)
    retVal = (ratio, centroid, eccentricity, solidity, skewness, kurtosis)
    return retVal

def getCSVFeatures(path, img=None, display=False):
    if img is None:
        img = mpimg.imread(path)
    temp = getFeatures(path, display=display)
    features = (temp[0], temp[1][0], temp[1][1], temp[2],
                temp[3], temp[4][0], temp[4][1], temp[5][0], temp[5][1])
    return features

def makeCSV():
    if not (os.path.exists('Dataset/Features')):
        os.mkdir('Dataset/Features')
        print('New folder "Features" created')
    if not (os.path.exists('Dataset/Features/Training')):
        os.mkdir('Dataset/Features/Training')
        print('New folder "Features/Training" created')
    if not (os.path.exists('Dataset/Features/Testing')):
        os.mkdir('Dataset/Features/Testing')
        print('New folder "Features/Testing" created')

    gpath = genuine_image_paths

    fpath = forged_image_paths
    n=0
    with open('users.csv', 'r+') as file:
        reader = csv.reader(file)
        n = len(list(reader))-1
        print(str(n)+'users')
        for person in range(1, n+1):
            per = ('00' + str(person))[-3:]
            print('Saving features for person id-', per)
            if not(os.path.exists('Dataset/Features/Training/training_' + per + '.csv')):
                with open('Dataset/Features/Training/training_' + per + '.csv', 'w') as handle:
                    handle.write(
                        'ratio,cent_y,cent_x,eccentricity,solidity,skew_x,skew_y,kurt_x,kurt_y,output\n')

                    for i in range(0, 3):
                        source = os.path.join(gpath, per + per + '_00' + str(i) + '.png')
                        features = getCSVFeatures(path=source)
                        handle.write(','.join(map(str, features)) + ',1\n')
                    for i in range(0, 3):
                        source = os.path.join(fpath, '021' + per + '_00' + str(i) + '.png')
                        features = getCSVFeatures(path=source)
                        handle.write(','.join(map(str, features)) + ',0\n')
            if not(os.path.exists('Dataset/Features/Testing/testing_' + per + '.csv')):
                with open('Dataset/Features/Testing/testing_' + per + '.csv', 'w') as handle:
                    handle.write(
                        'ratio,cent_y,cent_x,eccentricity,solidity,skew_x,skew_y,kurt_x,kurt_y,output\n')

                    for i in range(3, 5):
                        source = os.path.join(gpath, per + per + '_00' + str(i) + '.png')
                        features = getCSVFeatures(path=source)
                        handle.write(','.join(map(str, features)) + ',1\n')
                    for i in range(3, 5):
                        source = os.path.join(fpath, '021' + per + '_00' + str(i) + '.png')
                        features = getCSVFeatures(path=source)
                        handle.write(','.join(map(str, features)) + ',0\n')

def testing(path):
    feature = getCSVFeatures(path)
    if not (os.path.exists('Dataset/TestFeatures')):
        os.mkdir('Dataset/TestFeatures')
    with open('Dataset/TestFeatures/testcsv.csv', 'w') as handle:
        handle.write(
            'ratio,cent_y,cent_x,eccentricity,solidity,skew_x,skew_y,kurt_x,kurt_y\n')
        handle.write(','.join(map(str, feature)) + '\n')

def readCSV(train_path, test_path, type2=False):

    df = pd.read_csv(train_path, usecols=range(n_input))
    train_input = np.array(df.values)

    train_input = train_input.astype(np.float32, copy=False)
    df = pd.read_csv(train_path, usecols=(n_input,))
    temp = [elem[0] for elem in df.values]
    correct = np.array(temp)
    corr_train = keras.utils.to_categorical(
        correct, 2)

    df = pd.read_csv(test_path, usecols=range(n_input))
    test_input = np.array(df.values)
    test_input = test_input.astype(np.float32, copy=False)
    if not (type2):
        df = pd.read_csv(test_path, usecols=(n_input,))
        temp = [elem[0] for elem in df.values]
        correct = np.array(temp)
        corr_test = keras.utils.to_categorical(
            correct, 2)
    if not (type2):
        return train_input, corr_train, test_input, corr_test
    else:
        return train_input, corr_train, test_input

def multilayer_perceptron(x):
    layer_1 = tf.tanh((tf.matmul(x, weights['h1']) + biases['b1']))
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
    out_layer = tf.tanh(tf.matmul(layer_1, weights['out']) + biases['out'])
    return out_layer

def evaluate(train_path, test_path, type2=False):
    if not (type2):
        train_input, corr_train, test_input, corr_test = readCSV(
            train_path, test_path)
    else:
        train_input, corr_train, test_input = readCSV(
            train_path, test_path, type2)
    ans = 'Random'
    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(training_epochs):

            _, cost = sess.run([train_op, loss_op], feed_dict={
                X: train_input, Y: corr_train})
            if cost < 0.0001:
                break

        accuracy1 = accuracy.eval({X: train_input, Y: corr_train})

        if type2 is False:
            accuracy2 = accuracy.eval({X: test_input, Y: corr_test})
            return accuracy1, accuracy2
        else:
            prediction = pred.eval({X: test_input})
            print(prediction)
            if prediction[0][1] > prediction[0][0]:
                print('Genuine Image')
                ver = True
                return True
            else:
                print('Forged Image')
                ver = False
                return False

def trainAndTest(rate=0.001, epochs=1700, neurons=7, display=False):
    start = time()

    global training_rate, training_epochs, n_hidden_1
    learning_rate = rate
    training_epochs = epochs

    n_hidden_1 = neurons
    n_hidden_2 = 7
    n_hidden_3 = 30

    train_avg, test_avg = 0, 0
    n = 10
    for i in range(1, n + 1):
        if display:
            print("Running for Person id", i)
        temp = ('0' + str(i))[-2:]
        train_score, test_score = evaluate(train_path.replace(
            '01', temp), test_path.replace('01', temp))
        train_avg += train_score
        test_avg += test_score
    if display:

        print("Training average-", train_avg / n)
        print("Testing average-", test_avg / n)
        print("Time taken-", time() - start)
    return train_avg / n, test_avg / n, (time() - start) / n

def signrecog(uid, filename):
    global n_input
    n_input = 9
    global train_person_id
    train_person_id = uid
    global test_image_path
    test_image_path = filename
    global train_path
    train_path = 'Dataset/Features/Training/training_' + \
                 train_person_id + '.csv'
    testing(test_image_path)
    global test_path
    test_path = 'Dataset/TestFeatures/testcsv.csv'
    tf.reset_default_graph()

    global learning_rate
    learning_rate = 0.001
    global training_epochs
    training_epochs = 1000
    global display_step
    display_step = 1

    global n_hidden_1
    n_hidden_1 = 7
    global n_hidden_2
    n_hidden_2 = 10
    global n_hidden_3
    n_hidden_3 = 30
    global n_classes
    n_classes = 2

    global X
    X = tf.placeholder("float", [None, n_input])
    global Y
    Y = tf.placeholder("float", [None, n_classes])

    global weights
    weights = {
        'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], seed=1)),
        'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
        'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3])),
        'out': tf.Variable(tf.random_normal([n_hidden_1, n_classes], seed=2))
    }
    global biases
    biases = {
        'b1': tf.Variable(tf.random_normal([n_hidden_1], seed=3)),
        'b2': tf.Variable(tf.random_normal([n_hidden_2])),
        'b3': tf.Variable(tf.random_normal([n_hidden_3])),
        'out': tf.Variable(tf.random_normal([n_classes], seed=4))
    }

    global logits
    logits = multilayer_perceptron(X)

    global loss_op
    loss_op = tf.reduce_mean(tf.squared_difference(logits, Y))
    global optimizer
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    global train_op
    train_op = optimizer.minimize(loss_op)

    global pred
    pred = tf.nn.softmax(logits)
    global correct_prediction
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
    global accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    global init
    init = tf.global_variables_initializer()
    return evaluate(train_path, test_path, type2=True)

def register_validation(user):
    with open('users.csv', 'r+') as file:
        reader = csv.reader(file)
        for row in reader:
            if row[1] == user:
                return 1
    return 0

app = Flask(__name__)
app.secret_key = 'Examination Portal'

@app.route('/')
def start():
    return render_template("login.html")

#database = {'002': '123', 'abisheck': '123'}

@app.route('/login', methods=['POST', 'GET'])
def login():
    name1 = request.form['username']
    pwd = request.form['password']
    with open('users.csv', 'r+') as file:
        reader = csv.reader(file)
        a=0
        for row in reader:
            if a == 0:
                a+=1
                continue
            if row[1]==name1:
                if row[2]==pwd:
                    global session
                    session['username'] = name1
                    session['uid']=row[0]
                    return redirect("/cam", code=302)
                else:
                    return render_template('login.html', info='Invalid username or password')
    return render_template('login.html', info='Invalid username or password')

@app.route('/cam')
def cam():
    return (render_template("cam.html", user=session['username']))

@app.route('/capture', methods=['POST', 'GET'])
def capture():
    if usern == session['username']:
        return redirect("/upload", code=302)
    print(usern)
    print(session['username'])

@app.route('/sign', methods=['POST', 'GET'])
def sign():
    if request.method == 'POST':
        f = request.files['file']
        f.save(os.path.join('Images/temp.png'))
    print('saved')
    uid = session['uid']
    path =os.path.join('Images/temp.png')
    if signrecog(uid, path):
        os.remove(path)
        return redirect("/exam", code=302)
    else:
        return redirect("/error", code=302)

@app.route('/upload')
def upload():
    return (render_template("upload.html", user=session['username']))

@app.route('/exam')
def exam():
    return (render_template("exam.html", user=session['username']))

@app.route('/reg_image', methods=['POST', 'GET'])
def reg_image():
    camera.release()
    uname=session['username']
    regImage(uname)
    return redirect("/register3", code=302)

@app.route('/video_feed')
def video_feed():
    return Response(recog(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/register_video_feed')
def register_video_feed():
    return Response(cap(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/register1')
def register1():
    return (render_template("regi.html"))

@app.route('/register2', methods=['POST', 'GET'])
def register2():
    if(request.form['pass']==request.form['cpass']):
        global session
        session['username']=request.form['uname']
        session['password']=request.form['pass']
        flag=register_validation(session['username'])
        if flag == 1:
            return (render_template("regi.html", info='Username is not available'))
        global camera
        camera = cv2.VideoCapture(0)
        return (render_template("regi2.html"))
    else:
        return (render_template("regi.html",info='Passwords do not match'))

@app.route('/register3')
def register3():
    return (render_template("regi3.html"))

@app.route('/get_sign', methods=['POST', 'GET'])
def get_sign():
    if request.method == 'POST':
        files = request.files.getlist('files[]')
        with open('users.csv', 'r+',newline='') as file:
            reader = csv.reader(file)
            n = str(len(list(reader))).zfill(3)
            session['uid']=n
            writer = csv.writer(file,delimiter=',',lineterminator='\n')
            writer.writerow([n, session['username'],session['password']])
        print(len(files))
        uid=session['uid']
        for i in range(0,5):
            source = os.path.join(genuine_image_paths, uid + uid + '_00' + str(i) + '.png')
            files[i].save(source)
    print('saved')
    makeCSV()
    encode()
    return redirect("/success", code=302)

'''
@app.route('/get_sign', methods=['POST', 'GET'])
def get_sign():
    if request.method == 'POST':
        if request.form['submit']=='Submit':
            files = request.files.getlist('files[]')
            print(len(files))
            uid=session['uid']
            for i in range(0,5):
                source = os.path.join(genuine_image_paths, uid + uid + '_00' + str(i) + '.png')
                files[i].save(source)
            print('saved')
            makeCSV()
            encode()
            return redirect("/success", code=302)
        elif request.form['submit'] == 'Reset':
            f = open("users.csv", "r+")
            lines = f.readlines()
            lines = lines[:-1]
            writer = csv.writer(f, delimiter=',')
            for line in lines:
                writer.writerow(line)
            user=session['username']
            directory = user
            parent= "Images/"
            path=os.path.join(parent, directory)
            shutil.rmtree(path, ignore_errors=True)
            return redirect("/register1", code=302)
'''

@app.route('/success')
def success():
    return (render_template("success.html"))

@app.route('/logout')
def logout():
    global usern
    usern = "Unknown"
    session.pop('username', None)
    session.pop('uid', None)
    session.pop('password', None)
    return (render_template("login.html"))

@app.route('/error')
def error():
    return (render_template("error.html"))

@app.errorhandler(500)
def internal_error(error):
    return redirect("/error", code=302)

if __name__ == '_main_':
    app.run(debug=True)

max_stars_repo_name: abisheckkathir/Exam-Authenticator

max_issues_repo_name: abisheckkathir/Exam-Authenticator

max_forks_repo_name: abisheckkathir/Exam-Authenticator

reason: import numpy,from scipy


text:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import DetailView

from formtools.wizard.views import SessionWizardView
from sagepaypi.conf import get_setting
from sagepaypi.forms import CardIdentifierForm
from sagepaypi.models import Transaction

from example.forms import TransactionForm

class TransactionCreateView(SessionWizardView):
    form_list = [
        ('transaction', TransactionForm),
        ('card', CardIdentifierForm),
    ]
    initial_dict = {
        'transaction': {
            'amount': 100,
            'currency': 'GBP',
            'description': 'Payment for goods'
        }
    }
    templates = {
        'transaction': 'example/forms/transaction.html',
        'card': 'example/forms/card.html',
    }

    def get_template_names(self):
        return [self.templates[self.steps.current]]

    def done(self, form_list, form_dict, **kwargs):
        transaction = form_dict['transaction'].save(commit=False)

        card_identifier = form_dict['card'].save()

        # for new cards payment type is Payment
        transaction.type = 'Payment'
        transaction.card_identifier = card_identifier
        transaction.save()

        transaction.submit_transaction()

        if transaction.requires_3d_secure:
            return render(
                self.request,
                'sagepaypi/3d_secure_redirect_form.html',
                {'transaction': transaction}
            )

        transaction.refresh_from_db()
        tidb64, token = transaction.get_tokens()

        return HttpResponseRedirect(
            reverse(
                get_setting('POST_3D_SECURE_REDIRECT_URL'),
                kwargs={'tidb64': tidb64, 'token': token}
            )
        )

class TransactionStatusView(DetailView):
    template_name = 'example/transaction_status.html'
    model = Transaction

    def get_object(self):
        tidb64 = self.kwargs['tidb64']
        token = self.kwargs['token']
        return Transaction.objects.get_for_token(tidb64, token)

max_stars_repo_name: lejion/django-sagepaypi

max_issues_repo_name: lejion/django-sagepaypi

max_forks_repo_name: lejion/django-sagepaypi

reason: from sage


text:
import json
import csv
import os
from module.detector.dataset import Taco
from module.mask import Mask
from pycocotools.cocoeval import COCOeval
from pycocotools import mask as maskUtils
import numpy as np

ROOT_DIR = "module"
RCNN = Mask(
    PathToROOT=os.path.join(ROOT_DIR, "detector/models"),
    classMap=os.path.join(ROOT_DIR, "detector/taco_config/map_10.csv"),
    modelName="mask_02",
    pathToDataset=os.path.join(ROOT_DIR, "data"),
    splitnumber=2,
)
RCNN.detectBulkIMG("fileanhngoai2", "out2")


text:
import numpy as np

class RBF:
    def __init__(self, sigma):
        '''
        RBF-kernel
        :param sigma: float
        '''
        self._sigma = sigma

    def compute(self, x, y):
        '''
        :param x: array-like
            First vector.
        :param y: array-like
            Second vector.
        :return: float
        '''
        sqr_dist = sum([(it1 - it2)**2 for it1, it2 in zip(x,y)])
        return np.e**(-1.0 * (sqr_dist ** 2) / (2 * (self._sigma ** 2)))

    def display(self):
        return 'RBF('+str(round(self._sigma, 4))+')'

class Poly:
    def __init__(self, c, d):
        ''' Polynomial kernel

        :param c: float
            Parameter trading off the influence of higher-order versus lower-order terms in the polynomial.
        :param d: float
            Degree.
        '''
        self.__c = c
        self.__d = d

    def compute(self, x, y):
        '''
        :param x: array-like
            First vector.
        :param y: array-like
            Second vector.
        :return: float
        '''
        return (np.dot(x,y) + self.__c)**self.__d

    def display(self):
        return 'Poly(' + str(self.__c) + ', ' + str(self.__d) + ')'


text:
# -*- coding: utf-8 -*-
"""Knowledge_Distillation.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1OAkgOFYmfsD7Um1WiHnvFm_WfJa9XdIW
"""

#Importing necessary Libraries

import tensorflow as tf
import numpy as np
import keras
import random
from keras import backend as K
from keras.models import Sequential
from keras.models import Model
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM, GRU
from keras.layers import Input, Embedding, LSTM, Dense, Conv2D, Conv1D, AveragePooling1D, MaxPool2D,GlobalAveragePooling2D,MaxPool1D,GlobalAveragePooling1D,BatchNormalization,Flatten,Bidirectional,Conv3D,MaxPool3D,GlobalAveragePooling3D
from keras.optimizers import Adam
from keras.layers import concatenate
from keras.regularizers import l2
from keras.callbacks import ModelCheckpoint
from keras import optimizers, callbacks
from tensorflow.keras.utils import to_categorical
import random as rn

#Setting up the SEED
import os
os.environ['PYTHONHASHSEED']='0'
np.random.seed(13817)
rn.seed(12513)
tf.random.set_seed(8130)

scale_factor=4 # defining the scaling factor

def line1(x):
  # received shape: (256*8)

  conv_1=Conv1D(int(128),3,activation='relu',padding='same')(x)

  pool_1=MaxPool1D(2)(conv_1)
  print(pool_1.shape)
  reshaped_1=tf.reshape(pool_1,[tf.shape(x)[0],int(128),int(128),1])

  #2D net
  block1_conv1 = Conv2D(int(32/scale_factor),(7,7),activation='relu',padding='same')(reshaped_1)
  block1_pool  = MaxPool2D(2,2)(block1_conv1)

  block2_conv1 = Conv2D(int(64/scale_factor),(5,5),activation='relu',padding='same')(block1_pool)
  block2_pool  = MaxPool2D(2,2)(block2_conv1)

  #block3_conv1 = Conv2D(128,(5,5),activation='relu',padding='same')(block1_pool)
  #block3_pool  = MaxPool2D(2,2)(block3_conv1)

  global_pool= GlobalAveragePooling2D()(block2_pool)

  fully_connected_1= Dense(int(512/scale_factor),activation='relu')(global_pool)

  return fully_connected_1

def line2(x):
  # received shape: (64*16)

  conv_1=Conv1D(int(64),14,activation='relu',padding='same')(x)

  reshaped_1=tf.reshape(conv_1,[tf.shape(x)[0],int(64),int(64),1])
  print(reshaped_1.shape)
  '''block1_pool=MaxPool2D(4)(conv_1)
  print(block1_pool.shape)'''
  #2D net
  block1_conv1 = Conv2D(int(32/scale_factor),(7,7),activation='relu',padding='same')(reshaped_1)
  block1_pool  = MaxPool2D(2,2)(block1_conv1)

  block2_conv1 = Conv2D(int(64/scale_factor),(5,5),activation='relu',padding='same')(block1_pool)
  block2_pool  = MaxPool2D(2,2)(block2_conv1)

  # block3_conv1 = Conv2D(128,(5,5),activation='relu',padding='same')(block1_pool)
  # block3_pool  = MaxPool2D(2,2)(block3_conv1)

  global_pool= GlobalAveragePooling2D()(block2_pool)

  fully_connected_1= Dense(int(512/scale_factor),activation='relu')(global_pool)

  return fully_connected_1

def line3(x):
  # received shape: (16*32)

  conv_1=Conv1D(int(16),14,activation='relu',padding='same')(x)
  reshaped_1=tf.reshape(conv_1,[tf.shape(x)[0],int(16),int(16),1])
  block1_conv1 = Conv2D(int(16),(3,3),activation='relu',padding='same')(reshaped_1)
  #print(block1_conv1.shape)

  reshaped_2=tf.reshape(block1_conv1,[tf.shape(x)[0],int(16),int(16),int(16),1])
  conv_1=Conv3D(int(32/scale_factor),(2,2,2),activation='relu',padding='same')(reshaped_2)
  conv_1=MaxPool3D(2)(conv_1)
  conv_1=Conv3D(int(64/scale_factor),(2,2,2),activation='relu',padding='same')(conv_1)
  conv_1=MaxPool3D(2)(conv_1)
  conv_1=Conv3D(int(128/scale_factor),(2,2,2),activation='relu',padding='same')(conv_1)
  conv_1=MaxPool3D(2)(conv_1)
  conv_1=GlobalAveragePooling3D()(conv_1)
  out=Dense(int(512/scale_factor),activation='relu')(conv_1)
  return out

def line4(x):
  #received shape: (16*256)
  conv_1=Conv1D(int(512/scale_factor),14,activation='relu',padding='same')(x)
  conv_1=MaxPool1D(4)(conv_1)
  conv_1=BatchNormalization()(conv_1)
  lstm_1=LSTM(int(256/scale_factor),return_sequences=False)(conv_1)
  flat=Flatten()(lstm_1)
  out=Dense(int(512/scale_factor),activation='relu')(flat)
  #global_pool=GlobalAveragePooling1D()(conv_1)

  return out

def line5(x):
  x=BatchNormalization()(x)
  lstm1=(LSTM(int(128/scale_factor),return_sequences=False))(x)
  out=Dense(int(512/scale_factor),activation='relu')(lstm1)

  return out

def getModel():
  x=Input(shape=(1024,12),name='x')
  f_lstm=line5(x)

  # converting to (256*64):

  conv_1=Conv1D(int(64/scale_factor),21,activation='relu',padding='same',strides=1)(x)
  conv_1=MaxPool1D(4)(conv_1) # (256*64)
  print("YO1",conv_1.shape)

  f1=line1(conv_1)

  # converting to (64*128):

  conv_1=Conv1D(int(4),21,activation='relu',padding='same',strides=1)(conv_1)
  conv_1=MaxPool1D(4)(conv_1) # (32,64)

  f2=line2(conv_1)

  # converting to (16*256):

  conv_1=Conv1D(int(8),21,activation='relu',padding='same',strides=1)(conv_1)
  conv_1=MaxPool1D(4)(conv_1) # (8*128)

  f3=line3(conv_1)

  # converting to (16*256): # same input for line4 function

  f4=line4(conv_1)
  print(f4.shape)

  # concate the 4 dense layer channelwise

  f1=reshaped_1=tf.reshape(f1,[tf.shape(x)[0],int(128),1])
  f2=reshaped_1=tf.reshape(f2,[tf.shape(x)[0],int(128),1])
  f3=reshaped_1=tf.reshape(f3,[tf.shape(x)[0],int(128),1])
  f4=reshaped_1=tf.reshape(f4,[tf.shape(x)[0],int(128),1])
  f5=reshaped_1=tf.reshape(f_lstm,[tf.shape(x)[0],int(128),1])

  concat= tf.keras.layers.Concatenate()([f1,f2,f4,f5,f3])
  concat=tf.reduce_mean(concat,axis=2)

  dense_1=Dense(32,activation='relu')(concat)
  out=Dense(6,activation='softmax')(dense_1)

  #print(out.shape)
  model=Model(inputs=x,outputs=out)

  return model

model=getModel()
model.summary()

#Constructing Distiller() class
class Distiller(keras.Model):
    def __init__(self, student, teacher):
        super(Distiller, self).__init__()
        self.teacher = teacher
        self.student = student

    def compile(
        self,
        optimizer,
        metrics,
        student_loss_fn,
        distillation_loss_fn,
        alpha=0.1,
        temperature=3,
    ):
        """ Configure the distiller.

        Args:
            optimizer: Keras optimizer for the student weights
            metrics: Keras metrics for evaluation
            student_loss_fn: Loss function of difference between student
                predictions and ground-truth
            distillation_loss_fn: Loss function of difference between soft
                student predictions and soft teacher predictions
            alpha: weight to student_loss_fn and 1-alpha to distillation_loss_fn
            temperature: Temperature for softening probability distributions.
                Larger temperature gives softer distributions.
        """
        super(Distiller, self).compile(optimizer=optimizer, metrics=metrics)
        self.student_loss_fn = student_loss_fn
        self.distillation_loss_fn = distillation_loss_fn
        self.alpha = alpha
        self.temperature = temperature

    def train_step(self, data):
        # Unpack data
        x, y = data

        # Forward pass of teacher
        #teacher_predictions = self.teacher(x, training=False)
        teacher_layer_1 = K.function([self.teacher.input], [self.teacher.layers[-1].output,self.teacher.layers[-2].output,self.teacher.layers[-3].output,self.teacher.layers[-4].output,self.teacher.layers[-5].output,self.teacher.layers[-6].output,self.teacher.layers[-7].output,self.teacher.layers[-8].output,self.teacher.layers[-9].output])
        #teacher_layer_1 = K.function([self.teacher.input], [self.teacher.layers[-1].output])

        teacher_1 = teacher_layer_1([x])[0]
        teacher_2 = teacher_layer_1([x])[1]
        teacher_3 = teacher_layer_1([x])[2]
        teacher_4 = teacher_layer_1([x])[3]
        teacher_5 = teacher_layer_1([x])[4]
        teacher_6 = teacher_layer_1([x])[5]
        teacher_7 = teacher_layer_1([x])[6]
        teacher_8 = teacher_layer_1([x])[7]
        teacher_9 = teacher_layer_1([x])[8]

        with tf.GradientTape() as tape:
            # Forward pass of student
            #student_predictions = self.student(x, training=True)
            student_layer_1 = K.function([self.student.input], [self.student.layers[-1].output,self.student.layers[-2].output,self.student.layers[-3].output,self.student.layers[-4].output,self.student.layers[-5].output,self.student.layers[-6].output,self.student.layers[-7].output,self.student.layers[-8].output,self.student.layers[-9].output])
            #student_layer_1 = K.function([self.student.input], [self.student.layers[-1].output])

            ''',self.student.layers[-2].output,self.student.layers[-3].output,self.student.layers[-4].output]'''
            stu_1 = student_layer_1([x])[0]
            stu_2 = student_layer_1([x])[1]
            stu_3 = student_layer_1([x])[2]
            stu_4 = student_layer_1([x])[3]
            stu_5 = student_layer_1([x])[4]
            stu_6 = student_layer_1([x])[5]
            stu_7 = student_layer_1([x])[6]
            stu_8 = student_layer_1([x])[7]
            stu_9 = student_layer_1([x])[8]

            # Compute losses
            student_loss = self.student_loss_fn(y, student_predictions)

            '''distillation_loss = self.distillation_loss_fn(
                tf.nn.softmax(teacher_1 / self.temperature, axis=1),
                tf.nn.softmax(stu_1 / self.temperature, axis=1),
            )'''
            dis_loss_1=self.distillation_loss_fn(teacher_1,stu_1)
            dis_loss_2=self.distillation_loss_fn(teacher_2,stu_2)
            dis_loss_3=self.distillation_loss_fn(teacher_3,stu_3)
            dis_loss_4=self.distillation_loss_fn(teacher_4,stu_4)
            dis_loss_5=self.distillation_loss_fn(teacher_5,stu_5)
            dis_loss_6=self.distillation_loss_fn(teacher_6,stu_6)
            dis_loss_7=self.distillation_loss_fn(teacher_7,stu_7)
            dis_loss_8=self.distillation_loss_fn(teacher_8,stu_8)
            dis_loss_9=self.distillation_loss_fn(teacher_9,stu_9)
            distillation_loss=dis_loss_1+dis_loss_2+dis_loss_3+dis_loss_4+dis_loss_5+dis_loss_6+dis_loss_7+dis_loss_8+dis_loss_9
            #loss = self.alpha * student_loss + (1 - self.alpha) *dis_loss_1
            loss = self.alpha * student_loss + (1 - self.alpha) * distillation_loss
        # Compute gradients
        trainable_vars = self.student.trainable_variables
        gradients = tape.gradient(loss, trainable_vars)

        # Update weights
        self.optimizer.apply_gradients(zip(gradients, trainable_vars))

        # Update the metrics configured in `compile()`.
        self.compiled_metrics.update_state(y, student_predictions)

        # Return a dict of performance
        results = {m.name: m.result() for m in self.metrics}
        results.update(
            {"student_loss": student_loss, "distillation_loss": distillation_loss}
        )
        return results

    def test_step(self, data):
        # Unpack the data
        x, y = data

        # Compute predictions
        y_prediction = self.student(x, training=False)

        # Calculate the loss
        student_loss = self.student_loss_fn(y, y_prediction)

        # Update the metrics.
        self.compiled_metrics.update_state(y, y_prediction)

        # Return a dict of performance
        results = {m.name: m.result() for m in self.metrics}
        results.update({"student_loss": student_loss})
        return results

#loading teacher model
from keras.models import model_from_json
json_file = open('/content/drive/MyDrive/teacher_models/model_32.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
teacher = model_from_json(loaded_model_json)
# load weights into new model
teacher.load_weights("/content/drive/MyDrive/teacher_models/model_32.h5")
print("Loaded model from disk")


text:
# This module is to estimate the sequence or position biases. It provides
# ways to access and save the bias parameters.

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

def norm_pdf(x, mu, sigma):
    return 1 / (sigma*np.sqrt(2*np.pi)) * np.exp(-1/2*((x-mu)/sigma)**2)

class FastaFile:
    """docstring for FastaFile"""
    def __init__(self, fasta_file):
        self.f = pysam.FastaFile(fasta_file)

    def get_seq(self, qref, start, stop):
        """get the sequence in a given region, the start is from 1.
        The start and stop index may still need double check."""
        return self.f.fetch(qref, start-1, stop)

class BiasFile:
    """docstring for BiasFile"""
    def __init__(self, bias_file=None):
        """get the bias parameters from the hdf5 file"""
        self.set_base_chain()
        self.pos5_bias = np.zeros((5, 20))
        self.pos3_bias = np.zeros((5, 20))
        self.pos5_unif = np.zeros((5, 20))
        self.pos3_unif = np.zeros((5, 20))
        self.pos5_prob = np.zeros((5, 20))
        self.pos3_prob = np.zeros((5, 20))
        self.percentile = np.zeros((5, 2))
        self.flen_mean, self.flen_std = 0, 0
        self.flen_sum1, self.flen_sum2 = 0, 0
        self.read_num = 0

        self.seq5_bias, self.seq3_bias = {}, {}
        self.seq5_unif, self.seq3_unif = {}, {}
        self.seq5_prob, self.seq3_prob = {}, {}
        for i in range(len(self.chain_len)):
            self.seq5_bias[str(i)] = np.zeros(4**self.chain_len[i])
            self.seq3_bias[str(i)] = np.zeros(4**self.chain_len[i])
            self.seq5_unif[str(i)] = np.zeros(4**self.chain_len[i])
            self.seq3_unif[str(i)] = np.zeros(4**self.chain_len[i])
            self.seq5_prob[str(i)] = np.zeros(4**self.chain_len[i])
            self.seq3_prob[str(i)] = np.zeros(4**self.chain_len[i])

        if bias_file is None: return

        fid = open(bias_file, "r")
        all_lines = fid.readlines()
        fid.close()

        self.flen_mean = float(all_lines[4].split("\t")[0])
        self.flen_std  = float(all_lines[4].split("\t")[1])
        self.flen_sum1 = float(all_lines[4].split("\t")[2])
        self.flen_sum2 = float(all_lines[4].split("\t")[3])
        self.read_num  = float(all_lines[4].split("\t")[4])
        for i in range(5,105):
            a, b = (i-5) // 20, (i-5) % 20
            if b == 0:
                self.percentile[a,:] = all_lines[i].split("|")[0].split("-")
            self.pos5_bias[a,b] = all_lines[i].split("\t")[1]
            self.pos3_bias[a,b] = all_lines[i].split("\t")[2]
            self.pos5_unif[a,b] = all_lines[i].split("\t")[3]
            self.pos3_unif[a,b] = all_lines[i].split("\t")[4]
            self.pos5_prob[a,b] = max(0, self.pos5_bias[a,b] / self.pos5_unif[a,b])
            self.pos3_prob[a,b] = max(0, self.pos3_bias[a,b] / self.pos3_unif[a,b])
            # self.pos5_prob[a,b] = self.pos5_bias[a,b] / self.pos5_unif[a,b]
            # self.pos3_prob[a,b] = self.pos3_bias[a,b] / self.pos3_unif[a,b]

        ii, cnt = all_lines[105].split("|")[0], -1
        for i in range(105,849):
            if ii == all_lines[i].split("|")[0]:
                cnt += 1
            else:
                ii = all_lines[i].split("|")[0]
                cnt = 0
            self.seq5_bias[ii][cnt] = all_lines[i].split("\t")[1]
            self.seq3_bias[ii][cnt] = all_lines[i].split("\t")[2]
            self.seq5_unif[ii][cnt] = all_lines[i].split("\t")[3]
            self.seq3_unif[ii][cnt] = all_lines[i].split("\t")[4]
            self.seq5_prob[ii][cnt] = max(0, self.seq5_bias[ii][cnt] / self.seq5_unif[ii][cnt])
            self.seq3_prob[ii][cnt] = max(0, self.seq3_bias[ii][cnt] / self.seq3_unif[ii][cnt])
            self.base_chain[ii][cnt] = all_lines[i].split("\t")[0].split("|")[1]

    def add_bias_file(self, BF):
        self.pos5_bias += BF.pos5_bias
        self.pos3_bias += BF.pos3_bias
        self.pos5_unif += BF.pos5_unif
        self.pos3_unif += BF.pos3_unif
        for i in range(len(self.chain_len)):
            self.seq5_bias[str(i)] += BF.seq5_bias[str(i)]
            self.seq3_bias[str(i)] += BF.seq3_bias[str(i)]
            self.seq5_unif[str(i)] += BF.seq5_unif[str(i)]
            self.seq3_unif[str(i)] += BF.seq3_unif[str(i)]

        self.read_num  += BF.read_num
        self.flen_sum1 += BF.flen_sum1
        self.flen_sum2 += BF.flen_sum2
        if self.read_num > 0:
            self.flen_mean = self.flen_sum1 / (self.read_num+0.0)
            self.flen_std  = np.sqrt(self.flen_sum2*self.read_num - self.flen_sum1**2) / (self.read_num+0.0)

    def updata_prob(self):
        # self.pos5_prob = self.pos5_bias[a,b] / self.pos5_unif[a,b]
        # self.pos3_prob = self.pos3_bias[a,b] / self.pos3_unif[a,b]
        self.pos5_prob = self.pos5_bias / self.pos5_unif
        self.pos3_prob = self.pos3_bias / self.pos3_unif
        for i in range(len(self.chain_len)):
            self.seq5_prob[str(i)] = self.seq5_bias[str(i)] / self.seq5_unif[str(i)]
            self.seq3_prob[str(i)] = self.seq3_bias[str(i)] / self.seq3_unif[str(i)]
        self.flen_mean = self.flen_sum1 / (self.read_num+0.0)
        self.flen_std  = np.sqrt(self.flen_sum2*self.read_num - self.flen_sum1**2) / (self.read_num+0.0)

    def set_base_chain(self):
        """set the sub-base chain for the variable-length Markov model (VLMM),
        which was proposed by Reberts et al, Genome Biology, 2011:
        Figure2 in supp 3. http://genomebiology.com/2011/12/3/r22/"""
        b1 = ["A","T","G","C"]
        b2, b3 = [], []
        for i in b1:
            for j in b1:
                b2.append(j+i)
                for k in b1:
                    b3.append(k+j+i)
        base_comb = [b1, b2, b3]

        self.chain_len = [1]*4 + [2]*3 + [3]*10 + [2]*2 + [1]*2
        self.base_chain = {}
        for i in range(21):
            self.base_chain[str(i)] = base_comb[self.chain_len[i]-1]

    def get_both_bias(self, seq, loc, ulen, end_num=5):
        """get the bias from the bias parameters"""
        prob = (self.get_seq_bias(seq, end_num) *
                self.get_pos_bias(loc, ulen, end_num))
        return prob

    def get_seq_bias(self, seq, end_num):
        """get the sequence bias score"""
        if end_num == 5:
            parameters = self.seq5_prob
        elif end_num == 3:
            parameters = self.seq3_prob
        else:
            print("wrong end_num: %s" %str(end_num))
            return None

        prob = 1.0
        for j in range(len(seq)):
            _len = self.chain_len[j]
            _bas = seq[j-_len+1 : j+1]
            if self.base_chain[str(j)].count(_bas) == 0: continue
            _idx = self.base_chain[str(j)].index(_bas)
            prob = prob * parameters[str(j)][_idx]
        return prob

    def get_pos_bias(self, loc, ulen, end_num):
        """get the position bias score, the loc is base pair distance
        from the 5'end of the units"""
        if end_num == 5:
            parameters = self.pos5_prob
        elif end_num == 3:
            parameters = self.pos3_prob
        else:
            print("wrong end_num: %s" %str(end_num))
            return None

        bin1 = (ulen >= self.percentile[:,0]) * (ulen <= self.percentile[:,1])
        bin2 = 20.0 * loc / ulen
        prob = parameters[bin1, bin2]
        return prob

    def set_percentile(self, ulen, K=5):
        """set the percentiles by input the lengths of unitsets, i.e., ulen,
        and number of percentiles, K."""
        perc_gap = np.linspace(0, 100, K+1)
        _percent = np.percentile(ulen, list(perc_gap))
        self.percentile = np.zeros((K, 2))
        for i in range(K):
            self.percentile[i, 0] = int(_percent[i])+1
            self.percentile[i, 1] = int(_percent[i+1])
            if i == 0:
                self.percentile[i,0] = 0
            elif i==4:
                self.percentile[i,1] = float("inf")

    def set_both_bias(self, seq, loc, ulen, weight, end_num=5, mode="bias"):
        """get the bias from the bias parameters"""
        self.set_seq_bias(seq, weight, end_num, mode)
        self.set_pos_bias(loc, ulen, weight, end_num, mode)

    def set_seq_bias(self, seq, weight, end_num=5, mode="bias"):
        """get the sequence bias score"""
        for j in range(len(seq)):
            _len = self.chain_len[j]
            _bas = seq[j-_len+1 : j+1]
            if self.base_chain[str(j)].count(_bas) == 0: continue
            _idx = self.base_chain[str(j)].index(_bas)
            if end_num == 5:
                if mode == "bias":
                    self.seq5_bias[str(j)][_idx] += weight
                elif mode == "unif":
                    self.seq5_unif[str(j)][_idx] += weight
            else:
                if mode == "bias":
                    self.seq3_bias[str(j)][_idx] += weight
                elif mode == "unif":
                    self.seq3_unif[str(j)][_idx] += weight

    def set_pos_bias(self, loc, ulen, weight, end_num=5, mode="bias"):
        """get the position bias score, the loc is base pair distance
        from the 5'end of the units"""
        bin1 = (ulen >= self.percentile[:,0]) * (ulen <= self.percentile[:,1])
        bin2 = int(20.0 * loc / (ulen + 0.0001))
        if end_num == 5:
            if mode == "bias":
                self.pos5_bias[bin1, bin2] += weight
            elif mode == "unif":
                self.pos5_unif[bin1, bin2] += weight
        else:
            if mode == "bias":
                self.pos3_bias[bin1, bin2] += weight
            elif mode == "unif":
                self.pos3_unif[bin1, bin2] += weight

    def save_file(self, out_file="out_file.bias"):
        """to save the bias file in BIAS FILE FORMAT"""
        fid = open(out_file, "w")
        fid.writelines("# BIAS PARAMETER FORMAT\n")
        fid.writelines("# fragment leng: 5 (mean, std, sum_fl, sum_fl^2, reads), line 5\n")
        fid.writelines("# position bias: 5*20*4 (name, b5, b3, u5, u3), line 6-105\n")
        fid.writelines("# sequence bias: 744*4 (name, b5, b3, u5, u3), line 106-849\n")
        fid.writelines("%.2f\t%.2f\t%.2e\t%.2e\t%.0f\n" %(self.flen_mean, self.flen_std,
            self.flen_sum1, self.flen_sum2, self.read_num))
        for i in range(self.pos5_bias.shape[0]):
            for j in range(self.pos5_bias.shape[1]):
                aLine = ("%.0f-%.0f|%d\t%.2e\t%.2e\t%.2e\t%.2e\n"
                         %(self.percentile[i,0], self.percentile[i,1], j, self.pos5_bias[i,j],
                           self.pos3_bias[i,j], self.pos5_unif[i,j], self.pos3_unif[i,j]))
                fid.writelines(aLine)
        for i in sorted(self.base_chain.keys(), key=float):
            for j in range(len(self.base_chain[i])):
                aLine = ("%s|%s\t%.2e\t%.2e\t%.2e\t%.2e\n"
                         %(i, self.base_chain[i][j], self.seq5_bias[i][j],
                           self.seq3_bias[i][j], self.seq5_unif[i][j], self.seq3_unif[i][j]))
                fid.writelines(aLine)
        fid.close()

    def plot_bias(self, mode=None):
        """plot of bias parameters: flen, pos5, pos3, seq5, seq3"""
        #fragment distribution
        if mode == "flen":
            xx = np.arange(0, 1000)
            yy = norm_pdf(xx, self.flen_mean, self.flen_std)
            plt.fill(xx, yy, 'k')#, linewidth=2.0)
            plt.xlabel("fragment length")
            plt.ylabel("$p(L)$")
            plt.xlim(0, 400)

        #position bias
        if mode == "pos5" or mode == "pos3":
            plt.plot(np.arange(20), np.ones(20), '--k')
            for i in range(5):
                _label="bin%d: %.0f-%.0f bp" %(i+1, self.percentile[i,0], self.percentile[i,1])
                if mode == "pos5":
                    plt.plot(np.arange(20)+0.5, self.pos5_prob[i,:], linewidth=2.0, label=_label)
                else:
                    plt.plot(np.arange(20)+0.5, self.pos3_prob[i,:], linewidth=2.0, label=_label)
            plt.legend(loc="best")
            plt.xlabel("fractional transcription position")
            plt.ylabel("bias weight")
            plt.ylim(0,2)

        #sequence bias
        if mode == "seq5" or mode == "seq3":
            base = ["A", "T", "G", "C"]
            _color = ["g", "r", "orange", "b"]
            if mode == "seq5":
                plt.plot(np.arange(21)-8, np.ones(21), '--k')
                plt.plot(np.zeros(2), np.array([0, 2.0]), '--k', linewidth=2.0)
                percent = np.zeros((4,21))
                for i in range(4):
                    for j in range(21):
                        _seq_bias = self.seq5_prob[str(j)]
                        percent[i,j] = np.sum(_seq_bias[i*4**(self.chain_len[j]-1):
                            (i+1)*4**(self.chain_len[j]-1)]) / 4**(self.chain_len[j]-1)
                    plt.plot(np.arange(21)-8, percent[i,:], ":o", c=_color[i], label=base[i])
                plt.xlabel("offset from 3' fragment end")
                plt.xlim(-8,12)
            else:
                plt.plot(np.arange(21)-12, np.ones(21), '--k')
                plt.plot(np.zeros(2), np.array([0, 2.0]), '--k', linewidth=2.0)
                percent = np.zeros((4,21))
                for i in range(4):
                    for k in range(21):
                        j = 20 - k
                        _seq_bias = self.seq3_prob[str(j)]
                        percent[i,j] = np.sum(_seq_bias[i*4**(self.chain_len[j]-1):
                            (i+1)*4**(self.chain_len[j]-1)]) / 4**(self.chain_len[j]-1)
                    plt.plot(np.arange(21)-12, percent[i,:], ":o", c=_color[i], label=base[i])
                plt.xlabel("offset from 3' fragment end")
                plt.xlim(-12,8)
            plt.legend(loc="best")
            plt.xlabel("offset from %s' fragment end" %mode[3])
            plt.ylabel("bias weight")
            plt.ylim(0.5,2)

        #legend only
        if mode == "legend":
            base = ["A", "T", "G", "C"]
            _color = ["g", "r", "orange", "b"]
            plt.axis('off')
            ax1 = plt.twinx()
            for i in range(len(base)):
                ax1.plot([], [], "o", c=_color[i], label=base[i])
            ax1.legend(numpoints=1, loc=4)
            ax1.axis('off')
            ax2 = plt.twinx()
            for i in range(5):
                _label="bin%d: %.0f-%.0f bp" %(i+1, self.percentile[i,0], self.percentile[i,1])
                ax2.plot([], [], linewidth=2.0, label=_label)
            ax2.legend(loc=3)
            ax2.axis('off')

    def plot_bias_full(self):
        plt.subplot(3,2,1)
        self.plot_bias(mode="flen")
        plt.subplot(3,2,2)
        self.plot_bias(mode="legend")
        plt.subplot(3,2,3)
        self.plot_bias(mode="pos5")
        plt.legend().set_visible(False)
        plt.subplot(3,2,4)
        self.plot_bias(mode="pos3")
        plt.legend().set_visible(False)
        plt.subplot(3,2,5)
        self.plot_bias(mode="seq5")
        plt.legend().set_visible(False)
        plt.subplot(3,2,6)
        self.plot_bias(mode="seq3")
        plt.legend().set_visible(False)